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

别来无恙 提交于 2019-11-29 08:39:20

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1861875/upgrading-to-junit4-and-keeping-legacy-junit-3-tests-and-test-suites-by-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!