4

The application is throwing 2 different exceptions in some thatMethod(). I'm looking to test it by JUnit.

I can use ExpectedException and @Rule to set aside a "natural" exception the application is expected to throw. How do you assert that a certain exception is thrown in JUnit 4 tests? is explaining this.

How can i do that in my case-- 2 or more "natural" exceptions? ExpectedException is not holding multiple expected exceptions.

There are other ways to do this-- as explained in How do you assert that a certain exception is thrown in JUnit 4 tests? again. i'm wondering whether there is a way to do this by using the built-in features of JUnit.

Community
  • 1
  • 1
Roam
  • 4,831
  • 9
  • 43
  • 72
  • You could set a single common _parent_ exception type and then try to exploit [parametrized JUnit tests](https://github.com/junit-team/junit/wiki/Parameterized-tests). This would apply only if you're testing a single method (which by the way should really be the case). Not ideal but still it could work: I am using it to test the same exception thrown for different combinations of method parameters but without the need to setup separate tests. – Campa Mar 06 '15 at 17:12

3 Answers3

3

A single test method can only exit once, and so can only throw a single exception. If you wish to confirm that your code can fail in two different ways then you have two separate tests; split it into two test methods and declare the specific exception on each.

Joe
  • 29,416
  • 12
  • 68
  • 88
  • 1
    i am looking to set aside multiple expected exceptions-- so that test won't exit when they occur. – Roam Nov 26 '13 at 17:19
  • 2
    If you *really* need them in a single test, you'll need to use explicit `try`/`fail`/`catch` blocks, as in [this question](http://stackoverflow.com/questions/3083161/junit-exception-testing). You'll note that the highly-voted accepted answer to that question suggests breaking it up into separate tests; that would still be my recommendation too. – Joe Nov 27 '13 at 11:27
1

You can collect multiple exceptions with ErrorCollector and verify them at the end of the test. But you have to add the exceptions to the collector manually, because JUnit sees only your test method and cannot do anything during method execution.

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
  • ErrorCollector piles up the multiple AssertionErrors that the testcase is throwing. i'm looking to make JUnit identify the multiple expected errors the application can throw so that i can ignore them during the test. – Roam Nov 22 '13 at 09:52
1

I found this way because JUnit functions accepts only one exception.

@Test
public void checkRuleValidation() {
    Rule rule = generateRules(1, "rule.json").get(0);
    // rule name validation
    rule.setRuleName(null);
    try {
        ruleResource.saveRule(rule);
        // expecting exception if no assert false
        assertTrue(false);
    } catch (BadRequestException br) {
        assertTrue(true);
    }

    //validating rule action
    rule.setRuleAction(null);
    try {
        ruleResource.saveRule(rule);
        // expecting exception if no assert false
        assertTrue(false);
    } catch (AuthorizationException br) {
        assertTrue(true);
    }
}