Home > database >  How to invoke multiple methods in mockito spy in java
How to invoke multiple methods in mockito spy in java

Time:07-19

Say I have a java class like so

public class UnderTest {
   public void method1() { 
      callRealMethod();
  }
}

I want to create a spy object and modify the behavior of the method. I found a code example for doing it in in groovy using the Spock testing framework like this:

UnderTest underTest = Spy() {
      method1() >> {
        callRealMethod()
        timesExecuted  
      }
}

How can I do that in Java instead of Spock/Groovy?

CodePudding user response:

Use eg Mockito to count invocation count.

UnderTest spiedInstance=Mockito.spy(realInstance)

verify(spiedInstance,times(x)).method1();
  • Related