I'm having trouble calling the counting method using Mockito. The problem is that the whos calls method that I want to count is called in the test class indirectly by another method. Here is the code:
public class ClassForTest { private Integer value; public void doSmth() { prepareValue("First call"); prepareValue("Second call"); prepareValue("Third call"); System.out.println(value); } protected void prepareValue(String msg) { System.out.println("This is message: " + msg); value++; } }
And the test class:
public class ClassForTestTest extends TestCase { @Test public void testDoSmth() { ClassForTest testMock = mock(ClassForTest.class); doNothing().when(testMock).prepareValue(anyString()); testMock.doSmth(); verify(testMock, times(3)).prepareValue(anyString()); } }
Having such an exception:
Wanted but not invoked: classForTest.prepareValue(<any>); -> at org.testing.ClassForTestTest.testDoSmth(ClassForTestTest.java:24) However, there were other interactions with this mock: -> at org.testing.ClassForTestTest.testDoSmth(ClassForTestTest.java:21)
Any ideas please. Thanks in advance!
catdog
source share