0

I have a problem mocking the test, everything goes correct till this point of the code/test which i get a NullPointerException, this is the main code where launches the null pointer.

ResponseSrvAddDto responseSrvAdd = kjidR048.executeAddDocuments(requestAdd);
        
String returnCode = responseSrvAdd.getReturnCode();
if (returnCode != null && "00".equals(returnCode)){
ERROR! java.lang.NullPointerException

I have this in the test

ResponseSrvAddDto responseSrvAdd = mock(ResponseSrvAddDto.class);

Mockito.when(kjidR048.executeAddDocuments(requestAdd)).thenReturn(responseSrvAdd); (this goes correct)
String code = "00";
Mockito.when(responseSrvAdd.getReturnCode()).thenReturn(code); 

(but this looks like ignores the mock)

I don't know why having the mock in the responseSrvAdd.getReturnCode() launches a null pointer but i have this already mocked.

Lesiak
  • 22,088
  • 2
  • 41
  • 65
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stultuske Jul 09 '20 at 10:12
  • How do you know the first part "goes correct"? If you get a NullPointer on the code you show, that can only come from `responseSrvAdd` being `null`. – Thilo Jul 09 '20 at 10:14
  • did you check mock invocation happening or not? try printing the mock invocations – deadshot Jul 09 '20 at 10:21
  • Pls show requestAdd type. Did you implement equals? Is the requestAdd in test equal to requestAdd in method under test? Alternatively, check if argument matcher helps.`any()` is a good starting point. – Lesiak Jul 09 '20 at 11:27

1 Answers1

1

When you use mock on service it's create a object that do nothing or return null on all method. If you want to test responseSrvAdd.getReturnCode() use spy https://www.baeldung.com/mockito-spy here is guide.

For entity you need to create object it will be like this:

ResponseSrvAddDto responseSrvAdd = new ResponseSrvAddDto(); responseSrvAdd.setReturnCode("00"); Mockito.when(kjidR048.executeAddDocuments(requestAdd)).thenReturn(responseSrvAdd);

  • i have done this, and still getting nullpointerexception ResponseSrvAddDto responseSrvAddSpy = Mockito.spy(responseSrvAdd); responseSrvAddSpy.setReturnCode("00"); Mockito.verify(responseSrvAddSpy).setReturnCode("00"); – Frusciante xd Jul 09 '20 at 11:59
  • Wait you ResponseSrvAddDto it's just a entity, create object of ResponseSrvAddDto and return it in this moment when(kjidR048.executeAddDocuments(requestAdd)).thenReturn(responseSrvAdd) And verify() method check that method was called. User assertions for check that value correct. – Mikhail Bulanev Jul 09 '20 at 12:38
  • thats how i have it, but the problem is in the Mockito.when(responseSrvAddSpy.getReturnCode()).thenReturn("00");, but the whole code is as you mention, as i said, it looks like ignore the mockito.when(), but i already test the kjid call and its going well, just the second one, when i call to the getReturnCode() – Frusciante xd Jul 09 '20 at 12:59