加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – JUnit 5 – 使用JUnit Jupiter引擎时IntelliJ IDEA中的

发布时间:2020-12-14 06:00:30 所属栏目:Java 来源:网络整理
导读:如何在IntelliJ IDEA v2016.2.2中使用JUnit 5执行All Suite测试? 我得到运行此代码的Empty测试套件: import org.junit.platform.runner.IncludeEngines;import org.junit.platform.runner.JUnitPlatform;import org.junit.platform.runner.SelectPackages;
如何在IntelliJ IDEA v2016.2.2中使用JUnit 5执行All Suite测试?

我得到运行此代码的Empty测试套件:

import org.junit.platform.runner.IncludeEngines;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.runner.SelectPackages;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@IncludeEngines("junit-jupiter")
@SelectPackages("<eu...package>") //I confirm that <eu...package> is ok.
public class AllTests {
}

我收到:

INFORMAZIONI: Discovered TestEngines with IDs: [junit-jupiter,junit-vintage]
Empty test suite.
Empty test suite.

[root]
JUnit Jupiter
JUnit Vintage

要么

import eu.....services.ServiceTest;
import eu.....repository.DAOTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
        ServiceTest.class,DAOTest.class
})
public class AllTests {
}

我收到:

INFORMAZIONI: Discovered TestEngines with IDs: [junit-jupiter,junit-vintage]
Empty test suite.

[root]
|+--JUnit Vintage
|   +--eu.....AllTests
|+--JUnit Jupiter

我能够使用JUnit 4运行套件,但它不适用于JUnit 5.

解决方法

简答

如果您使用的是IntelliJ IDEA 2016.2,则目前无法在IDE中执行使用@RunWith(JUnitPlatform.class)注释的测试类.

答案很长

根据你报告的行为,经过一些艰苦的调查工作,我相信我有你的问题的答案……

如果您使用的是内置支持JUnit 5的IntelliJ IDEA 2016.2,那么以下是正在发生的事情.

> IDEA通过Launcher API启动JUnit平台,选择使用@RunWith(JUnitPlatform.class)注释的测试类(让我们称之为TestSuite).
> Launcher检测到junit-jupiter和junit-vintage TestEngine实现.
> JUnit Jupiter引擎忽略了TestSuite,因为它在技术上不是JUnit Jupiter测试类.
> JUnit Vintage引擎也忽略了TestSuite,因为它是用@RunWith(JUnitPlatform.class)注释的.
>最终结果是,既没有注册的测试引擎声称它可以运行TestSuite类.

非直观的部分是JUnit Vintage引擎忽略了TestSuite,它实际上看起来像一个基于JUnit 4的测试类,因为它是用@RunWith()注释的.忽略它的原因是避免无限递归,这在DefensiveAllDefaultPossibilitiesBuilder的源代码中有解释:

if ("org.junit.platform.runner.JUnitPlatform".equals(runnerClass.getName())) {
    return null;
}

上述代码在这种情况下返回null的事实导致空套件.

当然,如果用户被告知这种情况肯定会更好 – 例如,通过日志声明.因此,我已经为JUnit 5和IntelliJ提出了问题,以提高这种情况下的可用性.

从好的方面来说,由于您使用的是IntelliJ IDEA 2016.2,因此您无需使用测试套件支持.相反,您只需在IDEA的项目视图中右键单击src / test / java,然后选择Run’All Tests’,这将运行所有测试.

问候,

Sam(JUnit 5核心提交者)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读