-1

I need to write a unit test for the method processNotification. But this method internally calls JsonUtility.getNotificationDTOFromMessage.

I need the return value to be null when this is called. How can we achieve this by using Mockito?

I cannot make changes in the code as it is not owned by me.

@Override
public void processNotification(String message) throws IOException {
    logger.info(" Processing the notifications");
    NotificationDTO notification = 
    JsonUtility.getNotificationDTOFromMessage(message);
    if (null == notification) {
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Roshni
  • 137
  • 1
  • 2
  • 11
  • What have you tried, and what exactly is the problem with it? Have you read e.g. https://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito? Also note that `null == notification` is a "Yoda condition". – jonrsharpe Apr 20 '19 at 12:24
  • 1
    You can't. Mockito can't mock static methods. – JB Nizet Apr 20 '19 at 15:41

3 Answers3

1

You can’t mock static method by using Mockito. Try using the PowerMockito if you need to mock static methods

Ex: PowerMockito.mockStatic(JsonUtility.class);

0

You can't mock static metods using mockito.But you can manipulate your input data if possible, so that it will go into static method and return the value you want.you can pass message = null from your test method if possible.So that you can test your method.

indhu
  • 56
  • 5
0

Now newer version of Mockito supports static method mocking. No need to use PowerMockito Just Upgrade your version to 3.4.0 +

Onkar Patil
  • 80
  • 2
  • 11