0

In my test class, I have framed it as

public class TestDummy {

 private AClass a;
 private final HttpHeaders mockHeader = Mockito.mock(HttpHeaders.class);
 private final MultivaluedMap<String, String> mockMultiHeaderMap = 
 Mockito.mock(MultivaluedMap.class);

 @BeforeEach
 void beforeMethod(){
   Mockito.when(mockMultiHeaderMap.getFirst("abc")).thenReturn("def);
 }

 void actualTestClass(){
   Assertions.assertDoesNotThrow(() -> {
       return a.method(mockHeader);
   });
 }
}

This is my actual dummy class

public AClass dummy{
  public void method(HttpHeaders headers){
      MultivaluedMap<String, String> multiHeaderMap = headers.getRequestHeaders();
      String val = multiHeaderMap.getFirst("abc");
  }
}

I know that I am setting value to mockMultiHeaderMap but I am not getting that how to update my testcase since, in actual AClass we have to pass HttpHeaders object itself on method().

Rohit
  • 1
  • 1
  • I'm not sure I understood you correctly but shouldn't you be doing something like: Mockito.when(headers.getRequestHeaders()).thenReturn(aMap) where aMap can be a real map, it doesn't have to be a mock, there is actually no point in mocking a map since you can just create a map and insert anything there – Vitaly Chura Oct 20 '22 at 15:48
  • Thanks it worked for me @vitaly, I was doing wrongly. But then, how to put on private final MultivaluedMap = mockMultiHeaderMap = Mockito.mock(MultivaluedMap.class); – Rohit Oct 20 '22 at 16:03
  • Not sure I understood, where exactly do you want to make your map final? – Vitaly Chura Oct 20 '22 at 16:33
  • I was trying to use the headers.getRequestHeaders directly earlier. But now I don’t want to pass the headers directly but to put that to a map. Now, for testing out I wanted to ask that if I did correct changes by creating the mock of the map? If not, then what would be the correct approach – Rohit Oct 20 '22 at 17:52
  • Does this answer your question? [Why is my class not calling my mocked methods in unit test?](https://stackoverflow.com/questions/74027324/why-is-my-class-not-calling-my-mocked-methods-in-unit-test) – knittl Oct 20 '22 at 17:54
  • Your class under test is not using your mock. Besides, I would absolutely recommand against mocking simple data classes such as `Map` or `MultivaluedMap`. Just `new` up an instance and fill it with the data you need (`map.put(key, value)`) – knittl Oct 20 '22 at 18:04

0 Answers0