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:
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.
inside when() you don't call method on mock but on some other object.