Thursday, January 24, 2019

java - Testing Private method using mockito




public class A {

public void method(boolean b){

if (b == true)
method1();
else
method2();
}

private void method1() {}
private void method2() {}
}




public class TestA {

@Test
public void testMethod() {
A a = mock(A.class);
a.method(true);
//how to test like verify(a).method1();
}

}


How to test private method is called or not, and how to test private method using mockito???


Answer



You can't do that with Mockito but you can use Powermock to extend Mockito and mock private methods. Powermock supports Mockito. Here's an example.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...