How to run custom JUnit4 Runner on JUnit3 test classes using Ant?

We have test classes built on Spring 2.0.8 AbstractTransactionalDataSourceSpringContextTests. There are a huge number of them, all of which are written in the style of JUnit3.

To use JUnit 4 filtering, we came up with a replacement JUnit38Runner, which allows you to compare these tests with a specific application environment and filter them accordingly.

The entire test suite works just fine outside Ant, using the @RunWith annotation on our custom JUnit38Runner.

However, when we try to run in Ant, it forces individual tests to run as junit.framework.TestSuite or wrapped in a JUnit4TestAdapter, both of which ignore @RunWith annotations under JUnit4. Even worse, our existing packages are explicitly overridden by Ant, which calls the suite () methods directly rather than delegating to JUnit.

I tried to spread from Ant JUnitTestRunner and just override the run () method, however the class is simply not written for extension.

Besides copying the whole JUnitTestRunner and hacking it (which will open us problems with fragile code), is anyone lucky with other approaches to solving this problem?

+8
junit junit4 ant
source share
1 answer

We had a similar problem, and although it is not as clean as the junit task, it is not very difficult to solve. We created a class with main (), which simply calls Junit4Runner. It adds a RunListener, which attempts to write the junit output report in XML. The idea was that the data format was much less likely to change than a runner, so it was less fragile.

I have removed quite a lot of environmental code, but this is the main idea. Our test goal in ant is as follows:

  <java failonerror="yes" fork="true" classname="com.mycompany.test.Junit4Runner"> <classpath> <pathelement location="${basedir}/bin" /> <pathelement path="${ProjectTest.classpath}" /> <!-- above classpath includes junit-4.8.1.jar --> </classpath> <arg value="${test.class}" /> </java> 

You can view the code for the runner class here . It does not depend on anything outside of Java 6 SE and Junit 4.8, and can be compatible with Java 5 SE.

0
source share

All Articles