Best way to automatically port tests from JUnit 3 to JUnit 4?

I have a bunch of JUnit 3 classes that extend TestCase and want to automatically port them to JUnit4 tests with annotations like @Before , @After , @Test , etc.
Any tool for this in a great batch launch?

+43
java junit migration
Nov 05 '08 at 9:32 a.m.
source share
6 answers

In my opinion, this cannot be so difficult. So try:

0. Import

You need to import three annotations:

 import org.junit.After; import org.junit.Before; import org.junit.Test;` 

After making the following changes, you will not need import junit.framework.TestCase; .

1. Annotate test* Methods

All methods starting with public void test must be accompanied by @Test annotation. This task is simple with regex.

2. Annotate the SetUp and TearDown methods

Eclipse generates the following setUp() method:

 @Override protected void setUp() throws Exception { } 

Must be replaced by:

 @Before public void setUp() throws Exception { } 

Same for tearDown() :

 @Override protected void tearDown() throws Exception { } 

replaced by

 @After public void tearDown() throws Exception { } 

3. Get rid of extends TestCase

Delete exactly one entry in the line file

 " extends TestCase" 

4. Delete basic methods?

It may be necessary to remove / reorganize the existing core methods that will run the test.

5. Convert the suite() method to @RunWithClass

According to saua's comment, there should be a conversion of the suite() method. Thank saua

 @RunWith(Suite.class) @Suite.SuiteClasses({ TestDog.class TestCat.class TestAardvark.class }) 

Conclusion

I think it is very easy with a set of regular expressions, even if it kills my brain;)

+53
Mar 24 '09 at 13:19
source share

Here are the actual regular expressions that I used to execute the furtelwart sentences:

 // Add @Test Replace: ^[ \t]+(public +void +test) With: @Test\n $1 Regular Expression: on Case sensitive: on File name filter: *Test.java // Remove double @Test on already @Test annotated files Replace: ^[ \t]+@Test\n[ \t]+@Test With: @Test Regular Expression: on Case sensitive: on File name filter: *Test.java // Remove all empty setUp's Replace: ^[ \*]+((public|protected) +)?void +setUp\(\)[^\{]*\{\s*(super\.setUp\(\);)?\s*\}\n([ \t]*\n)? With nothing Regular Expression: on Case sensitive: on File name filter: *Test.java // Add @Before to all setUp's Replace: ^([ \t]+@Override\n)?[ \t]+((public|protected) +)?(void +setUp\(\)) With: @Before\n public void setUp() Regular Expression: on Case sensitive: on File name filter: *Test.java // Remove double @Before on already @Before annotated files Replace: ^[ \t]+@Before\n[ \t]+@Before With: @Before Regular Expression: on Case sensitive: on File name filter: *Test.java // Remove all empty tearDown's Replace: ^[ \*]+((public|protected) +)?void +tearDown\(\)[^\{]*\{\s*(super\.tearDown\(\);)?\s*\}\n([ \t]*\n)? With nothing Regular Expression: on Case sensitive: on File name filter: *Test.java // Add @After to all tearDown's Replace: ^([ \t]+@Override\n)?[ \t]+((public|protected) +)?(void +tearDown\(\)) With: @After\n public void tearDown() Regular Expression: on Case sensitive: on File name filter: *Test.java // Remove double @After on already @After annotated files Replace: ^[ \t]+@After\n[ \t]+@After With: @After Regular Expression: on Case sensitive: on File name filter: *Test.java // Remove old imports, add new imports Replace: ^([ \t]*import[ \t]+junit\.framework\.Assert;\n)?[ \t]*import[ \t]+junit\.framework\.TestCase; With: import org.junit.After;\nimport org.junit.Before;\nimport org.junit.Test;\nimport static org.junit.Assert.*; Regular Expression: on Case sensitive: on File name filter: *Test.java // Remove all extends TestCase Replace: [ \t]+extends[ \t]+TestCase[ \t]+\{ With: { Regular Expression: on Case sensitive: on File name filter: *Test.java // Look for import junit.framework; Find: import junit\.framework Manually fix Regular Expression: on Case sensitive: on // Look for ignored tests (FIXME, disabled, ...) Find: public[ \t]+void[ \t]+\w+test Manually fix Regular Expression: on Case sensitive: on // Look for dummy/empty tests Find: public[ \t]+void[ \t]+test[\w\d]*\(\s*\)\s*\{\s*(//[^\n]*)?\s*\} Manually fix Regular Expression: on Case sensitive: on 

Note: it is important to do this in the order shown above.

+34
Dec 26 2018-10-26
source share

We are in the middle of porting a fairly large JUnit4 code base. Since this is the second time I do such a migration, I decided to save the code somewhere:

https://github.com/FranciscoBorges/junit3ToJunit4

It addresses more angular cases than those listed in the answers above. For example:

  • calls TestCase.setUp() and TestCase.tearDown()
  • calls the TestCase(String) constructor inside the subclass constructor
  • calls the TestCase.assert* methods that have moved to Assert .
  • fix package names junit.framework to org.junit
  • etc.
+4
May 24 '13 at 9:10
source share

I don’t know a tool that would do this at the moment - I would expect Eclipse to provide some plugin quite shortly - but you can bring down a simple source tree exploring the Java class, which will do it for you if you just want to do a basic conversion . I had to write something similar to automatically generate skeletal test cases for an outdated application, so I already have enough support code. You can use it.

+3
Nov 05 '08 at 10:08
source share

As far as I know, there are no migration tools available (yet). I know it:

  • An article on API migration was published at OOPSLA in Nashville last year, but, alas, their tools did not seem to be openly available. I will provide a link to the article (although I dare to use it a little for you, since it is a rather difficult theory): "Annotation refactoring: Outputting transformations for legacy applications . "

  • Above, I wrote β€œthere is no tool available (yet)” because my student Leah Hansenberger is currently working on migrated APIs, not onyl, JUnit 4 a in JExample, and also from JUnit 3 to JUnit 4 Please follow JExample on Twitter to be notified when it releases the first beta.

I hope this information helps you.

+3
Mar 23 '09 at 9:26
source share

Good post. I upgraded using Netbeans with the following RegEx lines: (First search string, second substitute string)

 public void test @Test\n public void test @Override\n.*protected void onSetUp @Before\n protected void onSetUp @Override\n.*protected void onTearDown @After\n protected void onTearDown 

Remember to check the box next to Regular Expression.

+1
Jun 28 2018-10-06T00:
source share



All Articles