lundi 17 septembre 2018

Method not getting mocked

I am using Mockito to mock a method but the test is running the real method.

//Controller
@RestController
public class Controller {

    private Utils utils = new Utils();

    public String myMethod(String json){

        // Stuff gets done
        return utils.writeToKafka(topic, json, kafkatemplate, classname);

    }

I have a test class that looks like this:

//Test
@RunWith(SpringJUnit4ClassRunner.class)
public class ControllerTest {

    @Captor
    ArgumentCaptor<String> argumentCaptor;

    @Test
    public void processOSPUpdateRequested_test(){
         Controller controller = new Controller();
         Utils utils = Mockito.spy(new Utils());
         Mockito.doReturn("myResult").when(utils).writeToKafka(anyString(), anyString(), any(), anyString());

         String topic = controller.myMethod(myString);

         //Some assertions

My writeToKafka method signature is:

public String writeToKafka(String topic, String json, KafkaTemplate<String, String> kafkaTemplate, String classname)

However, when I run the test, writeTokafka is not mocked! It runs the actual method. Why is this happening? What am I missing?

Aucun commentaire:

Enregistrer un commentaire