Migration to JUnit 4

1 minute read

When you’re facing the issue to migrate your JUnit 3 tests to JUnit 4, it can be very annoying, because of course you all have a lot of tests. ;)

In general it is a mindless task to migrate things that have been changed due to a new major release.

For JUnit 4 one of the most popular features, which are new compared to JUnit 3, is the usage of Annotations and Pojos for test classes.

So what has to be done in order to migrate a TestCase:

  1. Remove the inheritance from TestCase
  2. Add static imports for org.junit.Assert methods, as these are not available from inheritance any more
  3. Make setUp() method public and add @Before annotation
  4. Make tearDown() method public and add @After annotation
  5. Add the @Test annotation to your test methods

Quite a lot of work, if you have to do it manually, isn’t it?

In order to automate this “mindless task” a bit, I started to write a migration plugin, which is available on GitHub[1] and also as p2 updatesite on bintray.com[2].

After you have installed this plugin you can trigger the follwing action on every Java project, java package or java class in the package explorer by selecting Soure > Convert to Junit 4.

Convert_to_JUnit4

As one of our aims at the vogella company is keeping the Eclipse code clean and modern the migration of the Eclipse tests to JUnit 4 is currently in progress.

 

[1] https://github.com/SimonScholz/codemodify - Github repo with Sources

[2] http://dl.bintray.com/vogellacompany/junit-4-tools/ - P2 updatesite to directly install it

Updated: