Test if another method was called

前端 未结 4 1559
生来不讨喜
生来不讨喜 2021-01-04 18:33

So I\'m sure there is something like this out there but I have been searching for an hour and haven\'t found exactly what I am looking for. say I have a class that looks lik

4条回答
  •  感动是毒
    2021-01-04 18:47

    Using Mockito, you can do spying on real objects like this:

    import org.junit.Test;
    import static org.mockito.Mockito.*;
    public class MyClassTest {
        @Test
        public void otherMethodShouldBeCalled() {
            MyClass myClass = new MyClass();
            MyClass spy = spy(myClass);
    
            spy.myMethod(true);
            verify(spy).otherMethod();
        }
    }
    

    There are some gotchas, so take a look at the relevant documentation as well.

提交回复
热议问题