mardi 11 février 2020

NPE when try to get null-property on object that exists, groovy

I try to test my service class with groovy and mongodb. Class CreateUserRequest contains only two fields.

@Subject
UserServiceImpl subject

@Collaborator
UserRepository userRepository = Mock()

def "should return created user model"() {
    given:
    def createUserRequest = new CreateUserRequest("Ivan_Ivanov@epam.com", "password")
    def model = new UserModel("id", "Ivan_Ivanov@epam.com", "login", "password", "firstName")

    when:
    subject.create(createUserRequest) // here i catch npe error

    then:
    1 * userRepository.save(model)
}

----

public UserModel create(CreateUserRequest createUserRequest) throws ApiException {
    UserEntity userEntity = getUserFromRequest(createUserRequest);
    if (Objects.nonNull(userEntity.getId()) && userRepository.exists(userEntity.getId())
            || Objects.nonNull(userRepository.findByEmail(userEntity.getEmail()))) {
        throw new SomeException();
    }
    return UserModel.from(userRepository.insert(userEntity)); // i catch error from this line
}

With debug mode i see null properties in such attributes UserEntity as id, login and firstName but this all have String type and in run mode it's none error.

Aucun commentaire:

Enregistrer un commentaire