Yassine Badache
Yassine Badache

Reputation: 1851

JUnit 4 : Expect one exception multiple times

I have the following piece of code:

@Test(expected = IllegalArgumentException.class)
public void failureTest()    {
    testedObject.supposedToFail("");
    testedObject.supposedToFail(null);
}

When running this, I have no guarantee that I will throw an IllegalArgumentException with a null parameter. Indeed, whenever JUnit meets the first exception, it stops the run of the whole method.

Considering the number of test cases I have to try for this class (~20), I doubt writing 20 methods each expecting a specific exception (which are often the same, tho) would be efficient.

Is there any way to try for every method throwing a specific exception at once ? (e.g. with my sample, you would go through both methods)

Thank you

Upvotes: 4

Views: 3094

Answers (3)

Bentaye
Bentaye

Reputation: 9756

I would use an auxiliary method and do it this way:

EDIT: This does not work, see alternative working solution

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

@Test
public void myTest() {
    List<Object[]> paramsList = Arrays.asList(
        new Object[] {"", IllegalArgumentException.class},
        new Object[] {null, NullPointerException.class});
    paramsList.forEach(a -> assertExceptionForParam((String)a[0], (Class)a[1]));
}

private void assertExceptionForParam(String param, Class expectedExceptionClass) {
    thrown.expect(expectedExceptionClass);
    testedObject.supposedToFail(param);
}

ALTERNATIVE WORKING SOLUTION, CHANGE AFTER COMMENT BY MIRZAK

My solution seems to actually only test the first case in the list. Here is a working version that will test them all

@Test
public void myTest() {
    List<Object[]> paramsList = Arrays.asList(
        new Object[] {null, NullPointerException.class},
        new Object[] {"", IllegalArgumentException.class},
        new Object[] {"zip", NullPointerException.class});
    paramsList.forEach(a -> assertExceptionForParam((String)a[0], (Class)a[1]));
}

private void assertExceptionForParam(String param, Class expectedExceptionClass) {
    boolean pass = false;
    try {
        testedObject.supposedToFail(param);
    } catch(Exception e) {
        pass = e.getClass() == expectedExceptionClass;
    }
    Assert.assertTrue("test failed for param:" + param + " and Exception "+expectedExceptionClass, pass);
}

This outputs, as expected:

java.lang.AssertionError: test failed for param:zip and Exception class java.lang.NullPointerException

Upvotes: 2

Maxim Logvinenko
Maxim Logvinenko

Reputation: 23

Unfortunately, auxiliary method won't work in that case, because @Test method will pass even if only one of auxiliary method invocations throws exception.

I didn't find a beautiful way to implement such test and end up with something like this:

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

@Test
public void testThrowIllegalArgumentExceptionForEmptyParam() {
    assertExceptionForParam("", IllegalArgumentException.class);
}

@Test
public void testThrowNullPointerExceptionForNullParam() {
    assertExceptionForParam(null, NullPointerException.class);
}

private void assertExceptionForParam(String param, Class expectedExceptionClass) {
    thrown.expect(expectedExceptionClass);
    testedObject.supposedToFail(param);
}

In case of test failure you will have clear understanding where and when it failed and also your code won't contain a lot of boilerplate.

Upvotes: 1

Dmytro Maslenko
Dmytro Maslenko

Reputation: 2297

Note, this is not a solution for junit but let me share how would be implemented the same test on spock:

def "supposedToFail(String) throws IllegalArgumentException when the given argument is null or blank"() {
    when:
    testedObject.supposedToFail(givenArgument)

    then:
    thrown(IllegalArgumentException)

    where:
    givenArgument << [null, '', '       ', '\n']
}

Spock can be used on your Java project together with junit without any problems.

Upvotes: -1

Related Questions