ExpectedException を使用して、特定のパラメーターを持つ例外がスローされたことを確認するにはどうすればよいですか。パラメータに基づいて異なるエラー メッセージを返す AcmeException があるとします。AcmeException(One) は One を返し、AcmeException(One,two,three) は Three を返します。テスト中に返されるメッセージには興味がありませんが、パラメーターリストの正確な例外には興味があります。
@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();
例外 (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);
}
}
上記の例外はここで呼び出されます。
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));
}
}
今テスト中に、いつ知りたいですか
ShortPasswordException(getMessage("error.password.length.case_digit_nondigit", lengthGp));
または
ShortPasswordException(getMessage("error.password.length.case_digit_nondigit", lengthGp))
グローバル プロパティの値を変更した後にスローされます。