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???
I don't really understand your need to test the private method. The root problem is that your public method has void as return type, and hence you are not able to test your public method. Hence you are forced to test your private method. Is my guess correct??
A few possible solutions (AFAIK):
Mocking your private methods, but still you won't be "actually" testing your methods.
Verify the state of object used in the method. MOSTLY methods either do some processing of the input values and return an output, or change the state of the objects. Testing the objects for the desired state can also be employed.
Refactor your code a little (Hope this is not a legacy code). My funda of writing a method is that, one should always return something (a int/ a boolean). The returned value MAY or MAY NOT be used by the implementation, but it will SURELY BE used by the test
code.
Think about this in terms of behaviour, not in terms of what methods there are. The method called
method
has a particular behaviour ifb
is true. It has different behaviour ifb
is false. This means you should write two different tests formethod
; one for each case. So instead of having three method-oriented tests (one formethod
, one formethod1
, one formethod2
, you have two behaviour-oriented tests.Related to this (I suggested this in another SO thread recently, and got called a four-letter word as a result, so feel free to take this with a grain of salt); I find it helpful to choose test names that reflect the behaviour that I'm testing, rather than the name of the method. So don't call your tests
testMethod()
,testMethod1()
,testMethod2()
and so forth. I like names likecalculatedPriceIsBasePricePlusTax()
ortaxIsExcludedWhenExcludeIsTrue()
that indicate what behaviour I'm testing; then within each test method, test only the indicated behaviour. Most such behaviours will involve just one call to a public method, but may involve many calls to private methods.Hope this helps.
By using reflection, private methods can be called from test classes. In this case,
//test method will be like this ...
If the private method calls any other private method, then we need to spy the object and stub the another method.The test class will be like ...
//test method will be like this ...
**The approach is to combine reflection and spying the object. **method1 and **method2 are private methods and method1 calls method2.
I was able to test a private method inside using mockito using reflection. Here is the example, tried to name it such that it makes sense
While Mockito doesn't provide that capability, you can achieve the same result using Mockito + the JUnit ReflectionUtils class or the Spring ReflectionTestUtils class. Please see an example below taken from here explaining how to invoke a private method:
Complete examples with ReflectionTestUtils and Mockito can be found in the book Mockito for Spring
Not possible through mockito. From their wiki