开发者

How can I run a custom JUnit4 Runner on JUnit3 test classes with Ant?

开发者 https://www.devze.com 2023-02-27 08:58 出处:网络
We have test classes which are built on Spring 2.0.8\'s AbstractTransactionalDataSourceSpringContextTests. There are a huge number of these, all of which are written in JUnit3 style.

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

In order to use JUnit 4 filtering, we have concocted a replacement JUnit38Runner which allows us to match these tests to a specific application environment, and filter them out accordingly.

The whole test suite runs fine outside of Ant, by using the @RunWith annotation on our custom JUnit38Runner.

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

I have attempted to extend from the Ant JUnitTestRunner, and simply override the run() method, however the class is simply not written for exten开发者_开发技巧sion.

Aside from copying the whole of the JUnitTestRunner and hacking it (which will open us up to brittle code issues), has anyone had any luck with other approaches to solving this problem?


We had a similar problem, and although it's not as clean as running the junit task, it's not terribly difficult to solve. We created a class with a main() that simply invokes the Junit4Runner. It adds a RunListener that attempts to write out the junit report output in XML. The idea was that the dataformat is much less likely to change than the runner, so it's less brittle.

I've stripped out a fair amount of environment-specific code, but this is the basic idea. Our test target in ant looks like this:

    <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 doesn't depend on anything outside Java 6 SE and Junit 4.8, and it may be compatible with Java 5 SE.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号