Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops.

Creating Test Suites with jUnit 4

1 min read

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...).
comments powered by Disqus