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

226 Views Asked by At
@Test
    void checkPutProfileNotFound() {
        ProfileDto profileDto = new ProfileDto("[email protected]", "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 protected], 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 protected], name=abcd, dob=null, number=null)
);
1

There are 1 best solutions below

0
Mohammad Shahwez On BEST ANSWER

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().