mardi 13 août 2019

Mocking function in the same class java

I am writing unit tests for a class, and I need to mock a call to a method in the same class.

//My class:
public class Class {
   public void functionA(arguments){
      ...
      String s3 = functionB(s1, s2);
   }

   public String functionB(String s1, String s2){
     ...
     return s3;
   }
}



//My Test:
@Test
public void functionA_Test(){
   Class class = new Class(dependency);
   Class spyClass = spy(class);

   mockString = "this is a mock";
   when(spyClass.functionB(any(),any()).thenReturn(mockString);

   spyClass.functionA(arguments);
}

When I debug the test function, after reaching "when(spyClass.functionB(any(),any()).thenReturn(mockString);" , the program jumps to the main class, inside functionB, to run it. What I want is to automatically assign s3=mockString when calling functionB from functionA.

Aucun commentaire:

Enregistrer un commentaire