javaspringnullpointerexceptionmockitowebtestclient

Null Pointer Exception Mockito Argument(s) are different (WebTestClient Put bodyValue)


@Test
    void checkPutProfileNotFound() {
        ProfileDto profileDto = new ProfileDto("abcd@gmail.com", "abcd", null, null);
        when(profileService.putProfile(profileDto)).thenReturn(Mono.error(new NotFoundException()));
        webTestClient.put().uri("/profile/put")
                .bodyValue(profileDto)
                .exchange();
        verify(profileService, times(1)).putProfile(profileDto);
    }

Null Pointer Exception is caused and below error is also shown. This error is resolved if I replace profileDto object with any() in when() and verify(). I want to understand where exactly I am making mistake in my test case.

Argument(s) are different! Wanted:
com.example.profile.service.ProfileService#0 bean.putProfile(
    ProfileDto(email=abcd@gmail.com, name=abcd, dob=null, number=null)
);
-> at com.example.profile.controller.ProfileControllerTest.checkPutProfileNotFound(ProfileControllerTest.java:115)
Actual invocations have different arguments:
com.example.profile.service.ProfileService#0 bean.putProfile(
    ProfileDto(email=abcd@gmail.com, name=abcd, dob=null, number=null)
);

Solution

  • As pointed out in the comments section adding the equals method solved the problem because it was needed for comparing the objects in when() and verify().