lundi 5 septembre 2016

Mock a function called by other function which has been already mocked

I am trying to mock static function(getBatchId() and sendPost()) for following code :

public void doPost(){      
String batchId = Utility.getBatchId();
                    Post post = new Post(batchId, userId, message);
                    String postJson = Utility.toJson(post);
                          Chat.sendPost(url,postJson)
}

Unit testcase code for above method :

 mockStatic(Utility.class);
        when(Utility.getBatchId()).thenReturn("demoBatchId1234");

        mockStatic(Chat.class);
        when(Chat.sendPost(url,postJson))
                .thenReturn(CompletableFuture.supplyAsync(() -> HttpResponse.create()));

I am getting following exception at when(Utility.getBatchId()).thenReturn("demoBatchId1234"); while running test case :

org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:

  1. you stub either of: final/private/equals()/hashCode() methods. Those methods cannot be stubbed/verified. Mocking methods declared on non-public parent classes is not supported.

  2. inside when() you don't call method on mock but on some other object.

Aucun commentaire:

Enregistrer un commentaire