I have a bunch of JUnit 3 classes which extend TestCase and would like to automatically migrate them to be JUnit4 tests with annotations such as @Before
, @After
, @Test
, etc.
Any tool out there to do this in a big batch run?
views:
1718answers:
6I don't know of a tool that would do this at the moment - I'd expect Eclipse to provide some plugin fairly shortly - but you could knock up a simple source tree exploring Java class that would do it for you if you only want to do a basic conversion. I had to write something similar to automatically generate skeleton test cases for a legacy application so I've got a fair amount of the support code already. You're welcome to use it.
Not answering the question, but you do realize that you can run JUnit3 tests under JUnit4 without modification, yes?
There are, to my best knowledge, no available migration tools (yet). What I know is this:
Last year, at OOPSLA in Nashville, was a paper about API migration but alas their tools seems not be openly available. I'll provide the link to the paper, (even though I dare it is of little use for you since it is rather theory heavy): "Annotation Refactoring: Inferring Upgrade Transformations for Legacy Applications".
Above, I wrote "no available tool (yet)" because my student Lea Hänsenberger is currently working on an auotmated API migration from, not onyl, JUnit 4 a to JExample, but also from JUnit 3 to JUnit 4. Please follow JExample on Twitter to get notified when she releases a first beta.
I hope this information was of help for you.
In my opinion, it cannot be that hard. So let's try it:
@Test annotation
All methods beginning with public void test
must precede the @Test
annotation.
This task is easy with a regex.
Get rid of extends TestCase
Remove exactly one occurence per file of the string
" extends TestCase"
SetUp and TearDown methods
Eclipse generates following setUp()
method:
@Override
protected void setUp() throws Exception { }
Must be replaced by:
@Before
protected void setUp() throws Exception { }
Same for tearDown()
:
@Override
protected void tearDown() throws Exception { }
replaced by
@After
protected void tearDown() throws Exception { }
Imports
The imports has to be reorganized:
- Remove
import junit.framework.TestCase;
- Add
org.junit.*;
orimport org.junit.After; import org.junit.Before; import org.junit.Test;
Remove main methods?
Probably it's necessary to remove/refactor existing main methods that will execute the test.
Convert suite()
method to @RunWithClass
According to saua's comment, there must be a conversion of the suite()
method.
Pattern will follow.
Thanks, saua!
Conclusion
I think, it's done very easy via a set of regular expressions, even if it will kill my brain ;)
@Jeffery Fredrick
Using the old JUnit 3 classes with JUnit 4 without doing a full conversion causes other problems. For Example, the @BeforeClass was being ignored when I first converted to JUnit 4 and added the @BeforeClass annotation.
The story turns out to be is if JUnit 4 sees a subclass of junit.framework.TestCase it will run the tests in the old style, which means annotations like @BeforeClass get ignored.
Nice post. I did the upgrade using Netbeans with the following RegEx strings: (First line search-string, second one replace-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
Don't forget to flag the Regular Expression checkbox!