Suite Method for Backwards Compatibility
In Eclipse, I can right-click on a Java class and use Run As:JUnit Test in one of a few situations:
- The class inherits from TestClass
- The class has a public static Test suite() {..} method
- If I happen to be selecting a package I can do the same
Since few IDE’s are JUnit 4 aware, JUnit 4 offers a class for backwards compatibility. It is JUnit4TestAdapter. Simply adding the method shown below, lines 38 - 40, will provide backwards-compatibility with Eclipse 2.x and 3.x:
Interesting Lines
Line | Description |
4 | This is the import for the JUnit4TestAdapter. Of course, if your in Eclipse, you can just start typing JUnit4 then hit ctrl-space for command line completing (assuming JUnit 4 is in the project’s class path |
38 - 40 | Nearly boilerplate code. The only thing you’ll need to change is the parameter passed in to the constructor. Instead of TestVehicle.class, you’ll pass in YourClass.class. |
If you have added the suite method and at least one method with the @Test annotation, you are ready to run your first JUnit 4 test.
Comments