Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

java - Subclass TestCase and use JUnit 4 annotations

In order to test a Map class I have defined, I have extended MapInterfaceTest from the Google Collections library.

The problem is I want to add more tests to this class, and be able to ignore specific long-running or broken ones with the @Ignore annotation while developing, but because MapInterfaceTest extends TestCase, the tests are run in Junit3 compatibility mode, and the annotations are ignored.

Is there any way to get Junit to respect the annotations on a test class which extends TestCase?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this annotation at your test class.

@RunWith(JUnit4.class)
public class MyTestCase extends MapInterfaceTest {...}

This should force Junit to use Junit4 not Junit3 in the execution. Maybe that works for your Testcase.

However, JUnit4 will not pick up old-style "test*" test methods, as it relies on @Test annotations. If the class you're extending doesn't have these annotations and you can't add them directly, you can override the tests in order to add the annotations in the subclass, like so:

@Test
@Override
public void testFoo()
{
    super.testFoo();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...