How can I check that an Exception with specific parameters is thrown, using ExpectedException. Suppose I have AcmeException which returns different error messages based on the parameters: AcmeException(One) returns One while AcmeException(One,two,three) returns Three. I am not interested in their return messages while testing but rather the exact exception with it's parameter list.
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
@Verifies(value= "should fail with AcmeException", method = "acmeMethod(String, String, String)")
public void acmeMethod_shouldFailWithAcmeException()
throws Exception {
//TODO auto-generated
//Set some global paramater
//Here only the generic exception type is checked
exception.expect(AcmeException.class);
exception.expectMessage("Some Message returned by AcmeException");
new acmeMethod_shouldFailWithAcmeException();
Exception (ShortPasswordException):
/**
* Password exception when the length is less than the minimum allowed.
* <p>
* @since 1.5
*/
public class ShortPasswordException extends PasswordException {
private static final long serialVersionUID = 31620091002L;
public ShortPasswordException() {
super("error.password.length");
}
public ShortPasswordException(String message) {
super(message);
}
}
The above exception is called here:
if (StringUtils.isNotEmpty(lengthGp) && password.length() < minLength) {
if ("true".equals(caseGp) && "true".equals(digitGp) && "true".equals(nonDigitGp)) {
throw new ShortPasswordException(getMessage("error.password.length.case_digit_nondigit", lengthGp));
} else if ("true".equals(digitGp) && "true".equals(nonDigitGp)) {
throw new ShortPasswordException(getMessage("error.password.length.digit_nondigit", lengthGp));
} else if ("true".equals(caseGp) && "true".equals(nonDigitGp)) {
throw new ShortPasswordException(getMessage("error.password.length.case_nondigit", lengthGp));
} else if ("true".equals(caseGp) && "true".equals(digitGp)) {
throw new ShortPasswordException(getMessage("error.password.length.case_digit_nondigit", lengthGp));
} else if ("true".equals(nonDigitGp)) {
throw new ShortPasswordException(getMessage("error.password.length.nondigit", lengthGp));
} else if ("true".equals(digitGp)) {
throw new ShortPasswordException(getMessage("error.password.length.digit", lengthGp));
} else if ("true".equals(caseGp)) {
throw new ShortPasswordException(getMessage("error.password.length.case", lengthGp));
} else {
throw new ShortPasswordException(getMessage("error.password.length", lengthGp));
}
}
Now while testing I would like to know when
ShortPasswordException(getMessage("error.password.length.case_digit_nondigit", lengthGp));
Or
ShortPasswordException(getMessage("error.password.length.case_digit_nondigit", lengthGp))
Is thrown after having changed the values of the global properties.