How to create a JUnit 4 test suite to pass when expected exceptions are thrown
Asked Answered
U

1

3

I am writing a JUnit 4 test suite which runs some tests that check if an exception has been thrown. On their own, my tests look somewhat like this:

@RunWith(value=BlockJUnit4ClassRunner.class)
public class CreateCommandsExceptionsTest extends TestCase {
    Statistics statistics;
    @Before
    public void setUp(){
        ...
    }
        ...
    @Test (expected=StatsArrayTooShortException.class)
    public void testStatsArrayTooShortException(){
        ...
    }

The tests run fine on their own, but when I attempt to put them in a test suite, they fail because the exception that I am testing for is being thrown.

My test suite looks like:

@RunWith(value=BlockJUnit4ClassRunner.class)
public class UnitTestSuite {
public static testSuite(){
    TestSuite suite = new TestSuite("Test for unitTests");

    suite.addTestSuite(StatisticsTest.class);
    ...
    return suite;
}
}

If anyone could tell me how I should be setting up my test suite so that I can pass a test when I catch an expected exception, I would appreciate it.

Unwatched answered 4/4, 2011 at 9:1 Comment(0)
M
4

You are using the wrong syntax for Suites in Junit 4.

This is the right way:

@RunWith(Suite.class)
@Suite.SuiteClasses({
        JunitTest1.class,
        JunitTest2.class
})
public class JunitTest5 {
}
Mango answered 4/4, 2011 at 9:17 Comment(2)
That works! Thank you! Could you recommend any good documentation for JUnit? I can't seem to find anyUnwatched
junit.sourceforge.net - junit4 cookbook junit.org - main site for junitMango

© 2022 - 2024 — McMap. All rights reserved.