can we mock a private class in Mokito? - java

what I need to do is to mock method inside of my private class.
Any tips on how to do it?
public class myPublicClass {
//
private static class myPrivateCalss {
public static method1() {
//do some stuff
}
}
}

You must to use PowerMock to mock this private class.

Related

Stubing a static private method inside a Utils class with powermockito

I'm having the following flow:
manager.getObject.doSomthing();
Where doSomething() calls a static function from a Utils class, that in turn, calls a private static function e.g:
public class obj {
public void doSomething(){
Utils.do();
}
}
public class Utils {
public static void do(){
int test = doPrivate();
...
~do unrelated computation~
...
}
private static int doPrivate(){
return someComplexMethod();
}
}
And I would like to mock the doPrivate, so I would still be able to test the do() method
Any way to achieve it with powermockito?
Using powermock-api-mockito you can achieve this.
You can mock specific static method of a class. Below is the syntax:
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberModifier.stub;
stub(method(Utils.class, "doPrivate")).toReturn(response);

PowerMock - Mock a Singleton with a Private Constructor

I'm using PowerMock with EasyMock, and wondered how I might mock a singleton with a private constructor?
Let's say I have the following class:
public class Singleton {
private static Singleton singleton = new Singleton();
private Singleton() { }
public static Singleton getInstance() {
return singleton;
}
public int crazyServerStuff() { ... }
}
And a class which uses this:
public class Thing {
public Thing() {}
public int doStuff(Singleton s) {
return s.crazyServerStuff() + 42;
}
}
How might I mock the crazyServerStuff method?
I've tried the following:
#RunWith(PowerMockRunner.class)
#PrepareForTest(Singleton.class)
public class ThingTest extends AndroidTestCase {
#Test
public void testDoStuff() {
MemberModifier.suppress(MemberModifier.constructor(Singleton.class));
Singleton mockSingleton = PowerMock.createMock(Singleton.class);
...
}
}
But I get the error java.lang.IllegalArgumentException: No visible constructors in class Singleton
Does anyone know what I'm missing?
I don't think you should suppress the constructor, but rather mock it:
PowerMock.expectNew(Singleton.class).andReturn(mockObject)
https://code.google.com/p/powermock/wiki/MockConstructor
Sadly I don't think this is possible for Android - see this answer.
If you're not on Android, it looks like this is how you do it.

How can you access public methods from private class from a different class in java?

I just have a question, is there any way to access public methods from a class which is private from a different class? For Example the print method can be accessed from a different class since the class is private?
private class TestClass {
public void print() {
}
}
Yes there is.
You don't actually return an direct reference to your private class, since other classes can't use it. Instead, you extend some public class, and return your private class as an instance of that public class. Then any methods it inherited can be called.
public interface Printable {
void print();
}
public class Test {
public Printable getPrintable() {
return new PrintTest();
}
private class PrintTest implements Printable {
public void print() {
}
}
}
Test test = new Test();
test.getPrintable().print();
You can do that by extending that class with a public class. Or you can always use reflection!

Mock private final static object with Mockito/PowerMockito

I'm currently trying to mock a private final static object within a class. It doesnt seem like my object is being mocked properly.
Example:
Code: In main class
public class Main {
private final static serviceA obj = new serviceA();
public somemethod { return true; }
}
Mocked: In my test class I have
Public class TestMain {
private Main mainObj;
private static serviceA obj;
#Before
public void setupBeforeTest() {
obj = Mockito.mock(serviceA.class);
PowerMockito.whenNew(serviceA.class).withNoArguments().thenReturn(obj);
mainObj= Mockito.spy(new Main());
}
}
But obj doesnt return the values I specify when doing
Mockito.when(obj.returnsFalseMethod()).thenReturn(false);
and will actually run the code for obj.returnsFalseMethod().
Any advice would be helpful, also i cannot change any code in the Main class, thanks.
I didn't realize you have to prepare the class creating the object. I was preparing every class except the class instantiating the object.

extends of the class with private constructor

Suppose we have the following code:
class Test {
private Test() {
System.out.println("test");
}
}
public class One extends Test {
One() {
System.out.println("One");
}
public static void main(String args[]) {
new One();
}
}
When we create an object One, that was originally called the parent class constructor Test(). but as Test() was private - we get an error.
How much is a good example and a way out of this situation?
There is no way out. You have to create an available (protected, public or default) super constructor to be able to extend test.
This kind of notation is usually used in utility classes or singletons, where you don't want the user to create himself an instance of your class, either by extending it and instanciating the subclass, or by simply calling a constructor of your class.
When you have a class with only private constructors, you can also change the class to final because it can't be extended at all.
Another solution would be having a method in test which create instances of test and delegate every method call from One to a test instance. This way you don't have to extend test.
class Test {
private Test() {
System.out.println("test");
}
public static Test getInstance(){
return new Test();
}
public void methodA(){
//Some kind of implementation
}
}
public class One {
private final Test test;
One() {
System.out.println("One");
test = Test.getInstance();
}
public void methodA(){
test.methodA();
}
public static void main(String args[]) {
new One();
}
}
Make the constructor of test non-private or move One into test.
BTW, your sample code contains a few issues:
classes should be named title case (Test instead of test)
I'd suggest to make the One's constructor private unless it is called from a different class in the same package
Actually, I found there is a way out. Like this:
class Base {
private Base() {
}
public void fn() {
System.out.println("Base");
}
public static class Child extends Base {
public void fn() {
System.out.println("Child");
}
}
public static Base getChild() {
return new Child();
}
}
Now, you can use getChild() to get instance of the extended class.

Resources