Alex L
Alex L

Reputation: 163

Custom Exceptions

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.

Upvotes: 3

Views: 141

Answers (3)

Ankush soni
Ankush soni

Reputation: 1459

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.

Upvotes: 1

Vishal Zanzrukia
Vishal Zanzrukia

Reputation: 4973

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;
    }
}

Upvotes: 1

AlmasB
AlmasB

Reputation: 3407

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.

Upvotes: 6

Related Questions