0

In the testing method, there are methods of the same class. How to mock the methods of the same class?

An example is as follows.

I am testing method2. It involves method1 which is in the same class. How to mock this method1?

public class A
{
  public void method1(int a, int b){

  }

  public void method2(){

      int value = method1(10,20);
  }
}
abc123
  • 1
  • 2

1 Answers1

0

You can use a Spy to do this, in combination with doReturn to stub out the method for which you want to provide canned behavior. There are many examples available on how to use a spy. Check out these tutorials and documentation:

Example:

   List list = new LinkedList();
   List spy = spy(list);

   //Use doReturn to change the behavior of a method call
   doReturn("foo").when(spy).get(0);
Shawn
  • 8,374
  • 5
  • 37
  • 60