3

I'm trying to define my own custom exceptions. Basically i want to prevent a user to be created if the age is less than 16. Following some of the discussions / questions i've come up with this 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 a 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'm calling validate method in Services, where i try to create the user:

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

So that's what i have so far, i tried to test this without 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);
    }

Here's what i get:

Expected: (an instance of org.dnet.training.exceptions.TrainingException and exception with message a string containing "The user age is invalid") but: exception with message a string containing "The user age is invalid" message was null.

It's obvious that i'm missing something but i'm stuck, tried different stuff but without any success so far.

1
  • Java handles/catches exceptions by type and not by content (=code). So please have distinct exception classes if you expect your users to handle exceptions thrown by your code. The content of exceptions should only be informational and should never be required for evaluation. Jul 23, 2015 at 8:14

3 Answers 3

6

You create an exception by throw new TrainingException(Code.USER_INVALID_AGE); which doesn't set the message. In constructor of TrainingException call super(code.getMessage()); which will set the message for that instance of the exception.

1

Try to rewrite your custom exception like below, hope 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;
    }
}
0
1

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

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

it will work.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.