How to mock a single method in java

后端 未结 4 1819
执念已碎
执念已碎 2020-12-13 09:09

Is it possible to Mock a single method of a Java class?

For example:

class A {
    long method1();
    String method2();
    int method3();
}


// in         


        
4条回答
  •  猫巷女王i
    2020-12-13 09:40

    Use the spy() method from Mockito, and mock your method like this:

    import static org.mockito.Mockito.*;
    
    ...
    
    A a = spy(new A());
    when(a.method1()).thenReturn(10L);
    

提交回复
热议问题