子类TestCase并使用JUnit 4批注 [英] Subclass TestCase and use JUnit 4 annotations

查看:160
本文介绍了子类TestCase并使用JUnit 4批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了测试我定义的Map类,我扩展了

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

问题是我想向此类添加更多测试,并且在开发时可以使用@Ignore批注忽略特定的长期运行或损坏的测试,但是由于MapInterfaceTest扩展了TestCase,因此这些测试在Junit3兼容模式下运行,并且注释将被忽略.

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.

有什么方法可以使Junit尊重扩展TestCase的测试类上的注释吗?

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

推荐答案

在测试类中尝试使用此批注.

Try this annotation at your test class.

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

这将强制Junit在执行过程中使用Junit4而不是Junit3.也许适用于您的测试用例.

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

但是,JUnit4依赖于@Test批注,因此不会采用旧式的"test *"测试方法.如果您要扩展的类没有这些注释,并且您不能直接添加它们,则可以覆盖测试以在子类中添加注释,如下所示:

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();
}

这篇关于子类TestCase并使用JUnit 4批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆