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