Mockito argument null Assuming test specified the type and did not return

While working with JUnit, when I was using a framework called Mockito as a kind of mock object, it did not return Output as I expected and it melted for half a day, so a memorandum.

↓ Mockito introduction site. How to drink Mockito

Premise

For example, if you want to test the pattern that null is included in the argument of the getContent method of the Controller class </ b> in the following source. (The code is quoted and modified from the above site.)

getContent is a method that uses the Service class getContentById inside it, but when you want to test only the logic directly under getContent, you don't want to worry about the logic inside getContentById. Therefore, use Mockito to specify I / O that does not depend on the logic in getContentById, and perform an independent test for each class.

Controller.java


  public class Controller
  {
      private Service service = new Service();

      public String getContent(String id) {
          String content = service.getContentById(id);
          return content;
      }
  }

Service.java


  public class Service
  { 
     private Map contents = new HashMap();
  
     //constructor
     public Service(){ genContents(); }

     //Specify key and return value
     public String getContentById(String id) {
          String content;
          content = contents.get(id);
          return content;
     }

     //Create content
     public void genContents() {
          contents.put("1","Tanaka");
          contents.put("2","Saitou");
          contents.put("3","Yoshida");
          contents.put("4","Suzuki");
     }
   }

The following miscode

  public class Test{
     Controller controller = new Controller();

     //Make a dummy service, I do not know the contents
     Service service = mock(Service.class);
     // I/Only O is set separately
     doReturn("Content1").when(service).getContentById(Mockito.anyString());
}

Sentences starting with doReturn

"Return the string Content1 whenever it is called with any Int type as an argument."

However, in this assumption, since the argument of getContentId is null, anyString () is not specified and getContent does not return anything. Therefore, we will specify arguments in a broader sense.

Correct code below

     doReturn("Content1").when(service).getContentById(Mockito.any());

Now in the getContentById method

"Return the string Content1 when called because anything is fine"

You can add the setting. I was able to move it even if the argument was null.

In the actual business, I used ʻArgumentMatchers.any (~~~ .class)instead ofMockito.Any ~ ()` to mock the method with my own type argument, but this time I tried to write it with a String type pattern more easily.

Postscript

Recommended Posts

Mockito argument null Assuming test specified the type and did not return
Optional Type conversion and return value (null support)
null specifies the type
java.util.Optional class and null and not null