Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading to JUnit4 and keeping legacy JUnit 3 tests and test suites by running them together

I was surprised not to find the answer so far. If I am missing something basic I will be more than happy to know that.

There is a large legacy code base that was upgraded to Java 6 (from 1.4). Large number of JUnit 3 tests are present in the code and are organized into test suite that runs successfully with JUnit 4 default runner in Eclipse.

Now, I am adding new tests that are pure JUnit 4 tests (annotations, no TestCase, etc.). What would be a way of running both old JUnit 3 test suite and new JUnit 4 tests together?

like image 590
topchef Avatar asked Dec 07 '09 18:12

topchef


People also ask

What is difference between JUnit 3 and JUnit 4?

JDK required. JUnit 4 uses a lot from Java 5 annotations, generics, and static import features. Although the JUnit 3. x version can work with JDK 1.2+, this usage requires that the new version of JUnit be used with Java 5 or higher.

Is JUnit5 better than JUnit4?

Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.

How do I switch from JUnit4 to JUnit5?

Use Find Action with ⌘⇧A, or Ctrl+Shift+A, and type Migrate, to see migration options for the code. IntelliJ IDEA offers the option to migrate the code from JUnit4 to JUnit5. This migration is similar to what we did with the individual test class, but for all test classes. Press Run to see the refactoring preview.

How do I update JUnit?

The steps are as below: Project > Properties > Java Build Path > Libraries. Click "Add External JARs..." button at right side --> Select your preferred JUnit jar. Click OK button.


2 Answers

Just use 'JUnit4' test runner in your run configuration.

JUnit4 binaries have a backward compatibility layer that allows it to have both JUnit3 and JUnit4 style classes in the same test suite.

For command line builds just use JUnit4 jars instead of JUnit3.

This was done specifically to ease migration that you are doing now.

Also, it works fine in my project.

like image 134
Alexander Pogrebnyak Avatar answered Nov 07 '22 19:11

Alexander Pogrebnyak


The @RunWith(Suite.class) gives me opportunity to combine both JUnit 4 and JUnit 3 tests and test cases together:

@RunWith(Suite.class)
@Suite.SuiteClasses({
    ExampleOfJunit3TestSuite.class, 
    ExampleOfJUnit3TestCase.class, 
    ExampleOfJUnit4TestSuite.class,
    ExampleOfJUnit4TestCase.class})   
public class BothJUnit4and3TestSuite {
}

The BothJUnit4and3TestSuite runs all tests and test suites listed in @Suite.SuiteClasses.

like image 43
topchef Avatar answered Nov 07 '22 20:11

topchef