Creating Test Suites with jUnit 4

A while back I've posted about how to enable the automatic (jUnit 3.8-style) creation of test suites in Eclipse. That was necessary for Eclipse 3.1.2 and previous. With Eclipse 3.2 and newer in combination with jUnit 4 there is no such wizard like with jUnit 3.8 which automatically creates you the test suite class, but you can use annotations for defining the jUnit 4 tests to run.

So you basically create your test
public class MyJUnitTest{

@Before
public void setUp(){
//do your setup stuff
}

@After
public void tearDown(){
//clean up
//note setUp and tearDown can be named differently as you want
//I just find it more clear to stick to this naming convention
}

@Test
public void testSomething(){
//the test
}
}
...and then you define your test suite.
import org.somepackagename.someprog;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith( Suite.class )
@SuiteClasses( {MyJUnitTest.class} )
public class UnitTests {
}

Then you launch it like you would with any other normal unit test.

I like this grouping in test suites because in this way you go to your main package in your test project and just launch the tests you're interested (unit tests, integration tests...).
Kindle

Comments

0

Your ad here?