2

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.

tutak
  • 1,120
  • 1
  • 15
  • 28
  • 1
    I don't understand what you're asking. The last instruction posted doesn't even compile: it has a redundant parenthesis, and tries to call the method as if it was a constructor. Have you read http://junit.org/apidocs/org/junit/rules/ExpectedException.html? Because it explains everything, with examples. – JB Nizet Sep 06 '15 at 08:50
  • @JBNizet It's just sample code to explain the problem. In simple terms I have two versions of AcmeException, different by their parameters. RIght now testing only considers the exception without their parameters, I would like to have it check the exception plus the arguments list. – tutak Sep 06 '15 at 08:56
  • Parameters are passed when constructing the exception. Once JUnit has the exception, what should it check? Its message? Something returned by a getter on the exception? Show the code of the exception. Show the code throwing the exception. Explain with a concrete example what you want to check. – JB Nizet Sep 06 '15 at 08:58
  • @JBNizet Updated. Does it make sense now? – tutak Sep 06 '15 at 09:13
  • exception.getMessage() will return the message passed in the constructor of the exception. So, if the value returned by `getMessage("error.password.length.case_digit_nondigit", lengthGp)` is "Hello world", you can check that the exception message is indeed "Hello World", using `exception.expectMessage("Hello world")`. – JB Nizet Sep 06 '15 at 09:26
  • @JBNizet That is correct what I aimed to achieve was decouple tests from the text messages (which could be changed) and let them depend only on the global properties keys passed as arguments to the exception. – tutak Sep 06 '15 at 09:35
  • Then use `exception.expectMessage(getMessage("error.password.length.case_digit_nondigit", lengthGp))` – JB Nizet Sep 06 '15 at 09:36

0 Answers0