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?
I have a bunch of JUnit 3 classes which extend TestCase and would like
Share
In my opinion, it cannot be that hard. So let’s try it:
0. Imports
You need to import three annotations:
After you’ve made the next few changes, you won’t need
import junit.framework.TestCase;.1. Annotate
test*MethodsAll methods beginning with
public void testmust be preceded by the@Testannotation. This task is easy with a regex.2. Annotate SetUp and TearDown methods
Eclipse generates following
setUp()method:Must be replaced by:
Same for
tearDown():replaced by
3. Get rid of
extends TestCaseRemove exactly one occurence per file of the string
4. Remove main methods?
Probably it’s necessary to remove/refactor existing main methods that will execute the test.
5. Convert
suite()method to@RunWithClassAccording to saua’s comment, there must be a conversion of the
suite()method. Thanks, saua!Conclusion
I think, it’s done very easy via a set of regular expressions, even if it will kill my brain 😉