开发者

Not able to execute tests with @Test annotation when my test extends TestCase(Junit) in Eclipse

开发者 https://www.devze.com 2022-12-10 20:26 出处:网络
Not able to execute tests with @Test annotation when my test extends TestCase(Junit) in Eclipse It works fine when I am not extending 开发者_JAVA技巧from TestCase(jUnit), but my existing code extends

Not able to execute tests with @Test annotation when my test extends TestCase(Junit) in Eclipse

It works fine when I am not extending 开发者_JAVA技巧from TestCase(jUnit), but my existing code extends from TestCase hence I would like to keep that as it is.


Check that you are not using JUnit3 runner.

Open 'Run Configuration' for you test and on Test tab make sure you have JUnit 4 selected for your test runner.

IMHO, if you are retrofitting your test suite for JUnit4, just drop support for JUnit3.

BTW, if you switch to 'JUnit 4' runner it will still execute old JUnit3 classes ( you don't have to add @Test annotations to them ).


Tests that extend junit.framework. TestCase are considered to be JUnit3-style tests by the JUnit4 runner. This is correct.

To @Test annotation you need NOT extend junit.framework. TestCase and @Test annotation works fine


Tests that extend junit.framework.TestCase are considered to be JUnit3-style tests by the JUnit4 runner. See the source code for AllDefaultPossibilitiesBuilder for more details.

You could force JUnit4 to treat classes that extend TestCase as JUnit4-style tests by annotating the classes with @RunWith(JUnit4.class) but you almost certainly do not want to do this. If you do this, your test classes would still inherit all of the methods from junit.framework.TestCase but those methods wouldn't be called. In other words, you would inherit a lot of cruft.

Inheritance is often a poor way to share code, even in tests. If it's possible to put the shared code in another class and delegate directly from the test classes, do that.

If you use JUnit 4.7 or later, Rules provide another way to share code. Rules can be affected by or can affect the behavior of a test.

In very rare cases, shared code needed by multiple JUnit4 test classes can be shared in a Runner, but since a test class can only specify one runner, runners have many of the same disadvantages as base classes.

0

精彩评论

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