0

I have a method named registerCustomer in a service implementation class. I created a test case named testRegisterCustomerSuccess for this method in a class named UserServiceImplTest, where I mock repositories and dependencies using Mockito:

@Mock
private UserRepository userRepository;

@Mock
private UserRoleRepository userRoleRepository;

@Mock
private CustomerRepository customerRepository;

@Mock
private PasswordEncoder passwordEncoder;

@BeforeEach
public void setUp() {
    MockitoAnnotations.openMocks(this);
}

@Test
public void testRegisterCustomerSuccess() {
    // Mock data
    SignupRequest signupRequest = createSignupRequest();

    when(userRepository.findByUsernameOrEmail(signupRequest.getEmail())).thenReturn(Optional.empty());
    when(passwordEncoder.encode(signupRequest.getPassword())).thenReturn("encodedPassword");
    when(userRepository.save(any(User.class))).thenAnswer(invocation -> {
        User user = invocation.getArgument(0);
        user.setId(1L); // Simulate saving in repository
        return user;
    });
    when(userRoleRepository.save(any(UserRole.class))).thenAnswer(invocation -> {
        UserRole role = invocation.getArgument(0);
        role.setUserRoleId(1L); // Simulate saving in repository
        return role;
    });
    when(customerRepository.save(any(Customer.class))).thenAnswer(invocation -> {
        Customer customer = invocation.getArgument(0);
        customer.setId(1L); // Simulate saving in repository
        return customer;
    });

    // Call the service method
    assertDoesNotThrow(() -> userService.registerCustomer(signupRequest));

    // Verify interactions
    verify(userRepository).findByUsernameOrEmail(signupRequest.getEmail());
    verify(passwordEncoder).encode(signupRequest.getPassword());
    verify(userRepository).save(any(User.class));
    verify(userRoleRepository).save(any(UserRole.class));
    verify(customerRepository).save(any(Customer.class));
}

But when the debugger reaches the corresponding point in the original service implementation method:

User savedUser = userRepository.save(user);

The savedUser object becomes null, causing a NullPointerException in the next line:

UserRole customerRole = new UserRole(savedUser.getId(), EnumUserRole.ROLE_CUSTOMER.name());

set savedUser = null so give me nullPointerException

This issue also occurs in all JPA repository methods like findAll, save, findByFieldName, etc. Could someone please provide a solution for unit and Mockito testing in a Spring Boot application?

2
  • Have you placed a break point in your mock to ensure that it is executed? Anyway, it is quite certain that the issue is not in the code you posted, so it is not possible to give a definitive answer. Try creating a minimal code sample that reproduces your problem and then post that, if you don't figure out the issue while doing it.
    – Torben
    Commented Jun 19 at 9:35
  • 3
    How did you create userService in the test? You should inject mocks to it somehow
    – Alex
    Commented Jun 19 at 9:44

0

Browse other questions tagged or ask your own question.