I am trying to test whether a method calls another method within the same class. Example:
public class Foo { public void bar(String a){ switch(a) { case "callBaz": baz(a); break; case "callBat": bat(a); break; default: System.out.println("Input was " + a); } } public void baz(String b){ System.out.println(b); } public void bat(String c){ System.out.println(c); } } However if I try mockito verify on the class itself:
import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) public class FooTest { @Test public void testBazCalledFromBar(){ Foo foo = new Foo(); foo.bar("callBaz"); Mockito.verify(foo).baz("callBaz"); } } I get the exception:
org.mockito.exceptions.misusing.NotAMockException: Argument passed to verify() is of type Foo and is not a mock! Make sure you place the parenthesis correctly! See the examples of correct verifications: verify(mock).someMethod(); verify(mock, times(10)).someMethod(); verify(mock, atLeastOnce()).someMethod(); When using the mock: ...
@RunWith(SpringJUnit4ClassRunner.class) public class FooTest { @Test public void testBazCalledFromBar(){ Foo fooMock = Mockito.mock(Foo.class); fooMock.bar("callBaz"); Mockito.verify(fooMock).baz("callBaz"); } } I get the exception:
Wanted but not invoked: foo.baz("callBaz"); -> at com.mn.stagportal.view.employee.FooTest.testBazCalledFromBar(FooTest.java:15) However, there were other interactions with this mock: foo.bar("callBaz"); -> at com.mn.stagportal.view.employee.FooTest.testBazCalledFromBar(FooTest.java:13) Does anyone know how to test if baz("callBaz") is called?
barhas the right effect; not which secondary methods it uses to achieve that effect. Your unit tests shouldn't depend on a particular implementation of a method - this isn't the right way to test.