Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better, ExpectedException or @Test(expected= [closed]

I have code where I check the exceptions in jUnit. I want to know which of the following is a good jUnit practice?

First

@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void checkNullObject() throws CustomException {
    exception.expect(CustomException.class);
    MyClass myClass= null;
    MyCustomClass.get(null);
}

Second

@Test(expected=CustomException.class)
public void checkNullObject() throws CustomException {
    MyClass myClass= null;
    MyCustomClass.get(null);    
}

EDIT: Please note that CustomException is a unchecked custom exception. (Though it won't have any impact on this question).

like image 523
Priyank Doshi Avatar asked Jul 11 '12 05:07

Priyank Doshi


People also ask

What is @test expected?

Example@Test(expected=IllegalArgumentException.class)By using “expected” parameter, you can specify the exception name our test may throw. In above example, you are using “IllegalArgumentException” which will be thrown by the test if a developer uses an argument which is not permitted.

How do you test a method that throws an exception?

You can use the expected field in the @Test annotation, to tell JUnit that this test should pass if the exception occurs. In this case, the tested method should throw an exception, so the test will pass. If you remove the expected = Exception. class from the annotation, the test will fail if an exception occurs.

What is expected exception?

The ExpectedException rule allows you to verify that your code throws a specific exception.


1 Answers

It depends what you want to check in the exception. If all you're doing is checking that the exception is thrown, then using @Test(expected=...) is probably the easiest way:

@Test(expected=CustomException.class)
public void checkNullObject() throws CustomException {
  MyClass myClass= null;
  MyCustomClass.get(null);
}

However, the @Rule ExpectedException has a lot more options, including checking the message, from the javadoc:

// These tests all pass.
public static class HasExpectedException {
    @Rule
    public ExpectedException thrown= ExpectedException.none();

    @Test
    public void throwsNothing() {
        // no exception expected, none thrown: passes.
    }

    @Test
    public void throwsNullPointerException() {
        thrown.expect(NullPointerException.class);
        throw new NullPointerException();
    }

    @Test
    public void throwsNullPointerExceptionWithMessage() {
        thrown.expect(NullPointerException.class);
        thrown.expectMessage("happened?");
        thrown.expectMessage(startsWith("What"));
        throw new NullPointerException("What happened?");
    }

    @Test
    public void throwsIllegalArgumentExceptionWithMessageAndCause() {
        NullPointerException expectedCause = new NullPointerException();
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("What");
        thrown.expectCause(is(expectedCause));
        throw new IllegalArgumentException("What happened?", cause);
    }
}

So you can check for the message, the original cause of the exception. For checking the message, you can use matchers, so you can check startsWith() and similar.

One reason to use the old style (Junit 3) throw/catch is if you have specific requirements. There aren't many of these, but it can happen:

@Test
public void testMe() {
    try {
        Integer.parseInt("foobar");
        fail("expected Exception here");
    } catch (Exception e) {
        // OK
    }
}
like image 132
Matthew Farwell Avatar answered Oct 17 '22 19:10

Matthew Farwell