Custom exceptions

I am trying to define my own custom exceptions. Basically I want to prevent the user from being created if the age is less than 16. Following some discussions / questions I have come up with so far.

public enum Code {

    USER_INVALID_AGE("The user age is invalid");

    private String message;

    Code(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

}

      

Exception class:

public class TrainingException extends RuntimeException {

private Code code;

    public TrainingException(Code code) {
        this.code = code;
    }

    public Code getCode() {
        return code;
    }

    public void setCode(Code code) {
        this.code = code;
    }
}

      

In the Validator package I have the following:

public class UserValidator implements Validator<User> {

    /** {@inheritDoc} */
    @Override
    public void validate(User type) {
        if (DateUtils.getYearDifference(type.getUserDetails().getBirthDate(), new DateTime())< 16) {
            throw new TrainingException(Code.USER_INVALID_AGE);
        }
    }
}

      

I am calling a validation method on services where I am trying to create a user:

public User save(User user) {
        validator.validate(user);
        return userRepository.save(user);
    }

      

So what I have so far, I have tried to test this with no success.

@ Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void testInvalidAge() throws TrainingException{

        thrown.expect(TrainingException.class);
        thrown.expectMessage(Code.USER_INVALID_AGE.getMessage());

        User user = userService.findAll().get(0);
        UserDetails userDetails = new UserDetails();
        userDetails.setBirthDate(UserTestUtils.buildDate(2000, 7, 21, 1, 1, 1));
        user.setUserDetails(userDetails);

        userService.save(user);
    }

      

This is what I get:

Expected: (example org.dnet.training.exceptions.TrainingException and exception with enter a string containing "User's age is not valid") but: The message exception contains a string with an invalid value "User's age is not valid".

Obviously I missed something, but I got stuck, tried different things but had no success yet.

+3


source to share


3 answers


You are throwing an exception with throw new TrainingException(Code.USER_INVALID_AGE);

that does not set the message. In the constructor, TrainingException

call super(code.getMessage());

that will set the message for this exception instance.



+6


source


Try to rewrite your own exception like below, hoping it helps :)



public class TrainingException extends RuntimeException {

    private Code code;

    public TrainingException(Code code) {
        super(code.getgetMessage());
        this.code = code;
    }

    public Code getCode() {
        return code;
    }

    public void setCode(Code code) {
        this.code = code;
    }
}

      

+1


source


In the TrainingException constructor, first call super (code.name ()) and then call this.code = code ie

public TrainingException(Code code) {super(code.name()) this.code = code;}

      

it will work.

+1


source







All Articles