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

后端 未结 3 1610
无人及你
无人及你 2021-01-16 14:04

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

It works fine when I am not extending from TestCase(jUnit), but my ex

相关标签:
3条回答
  • 2021-01-16 14:33

    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

    0 讨论(0)
  • 2021-01-16 14:41

    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 讨论(0)
  • 2021-01-16 14:43

    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 ).

    0 讨论(0)
提交回复
热议问题