简体   繁体   English

Junit 5 在 maven 构建中执行测试,即使没有注释

[英]Junit 5 performing tests in maven build even when not annotated

While preparing a release on maven ( release:prepare ) I've commented some tests I don't want to perform, but they are executed anyway, even completely removing the @Test annotation form the method.在准备maven ( release:prepare ) 上的发布时,我评论了一些我不想执行的测试,但它们无论如何都会执行,甚至完全从方法中删除@Test注释。

NOTE: Consider that the Skip Test option is ignored during maven release注意:考虑在 maven 发布期间忽略跳过测试选项

The build was performed under Eclipse wit the following environment :构建在 Eclipse 下执行,环境如下
- Apache Maven 3.5.4 external run-time (not the one embedded in Eclipse) - Apache Maven 3.5.4 外部运行时(不是嵌入在 Eclipse 中的运行时)
- Java JDK 1.8.0_151 - Java JDK 1.8.0_151
- maven-compiler-plugin 3.0 - maven-compiler-plugin 3.0
- maven-release-plugin 2.5.3 - maven-release-plugin 2.5.3
- junit-jupiter-api 5.5.2 -junit-jupiter-api 5.5.2
- junit-jupiter-engine 5.5.2 - junit-jupiter-engine 5.5.2

Usually to skip a test I comment only the annotation:通常要跳过测试,我只评论注释:

    //@Test
    public void testIwantToSkip() {
        assertTrue(true);
    }

At the moment the only way I've found to skip a test is to comment the whole method, but I expect it to be skipped only commenting the annotation, like it happened on Junit 4.目前我发现跳过测试的唯一方法是评论整个方法,但我希望它只在注释注释时被跳过,就像它发生在 Junit 4 上一样。

UPDATE更新
I've also tried to use the @Disable annotation, unfortunately with no result.我也尝试使用@Disable注释,不幸的是没有结果。 Sample:样本:

@Test
@Disabled
public void testAuditMapper() {
    ....
}

POSSIBLE SOLUTION可能的解决方案
After several tests and thanks to Kayaman hint I've figured out that under Maven build the annotations are totally ignored, only methods that start with " test " will be executed, the others will be totally ignored, regardless of the annotations.经过几次测试并感谢 Kayaman 的提示,我发现在 Maven 构建下,注释被完全忽略,只有以“ test ”开头的方法将被执行,其他方法将被完全忽略,无论注释如何。 In example:例如:

@Test
public void someThing() {
    // will NOT be executed....
}

@Disabled
public void testSomeThing() {
    // will be executed....
}

DEFINITIVE SOLUTION最终解决方案
In the build section of my pom the surefire plugin was not specified, so the resulting pom was using the 2.12.4 version of this plugin.在我的 pom 的构建部分中,没有指定 surefire 插件,因此生成的 pom 使用的是该插件的 2.12.4 版本。 By adding this in the build section:通过在构建部分添加:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
</plugin>

everything works as expected.一切都按预期工作。 Thanks @asa for hint.感谢@asa 的提示。

Try using @Ignore annotation.尝试使用 @Ignore 注释。

@Test
@Ignore("optional comment explaning the reason")
public void testIwantToSkip() {
    assertTrue(true);
}

Regarding your note:关于你的笔记:

For the release plugin, you need to specify all options for the inner build indirectly, by wrapping them into对于发布插件,您需要通过将它们包装成间接指定内部构建的所有选项

-Darguments="..."

If you are experiencing the issue during the release:perform goal, you will not be able to change your java class.如果您在发布期间遇到问题:执行目标,您将无法更改 java class。 Normally the perform goal takes the class committed in your SCM during the release:prepare phase.通常,执行目标采用在发布:准备阶段在您的 SCM 中提交的 class。 Your only options at this stage would be disable all the tests during the perform stage or rollback the commits from the prepare goal and use the conditional test features of Junit5 to condition their executions.在这个阶段你唯一的选择是在执行阶段禁用所有测试或从准备目标回滚提交,并使用 Junit5 的条件测试特性来调节它们的执行。

UPDATED Solution:更新的解决方案:

The maven project was using the default version of the surefire plugins (used for units tests) and not a more recent one which resolves some issues with JUnit 5. maven 项目使用的是默认版本的 surefire 插件(用于单元测试),而不是解决 JUnit 5 的一些问题的较新版本。

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>3.0.0-M3</version>
</plugin>

Note: the use of the following plugin is very handly to ensure a project use the latest version of dependencies but most importantly the latest build ones:注意:使用以下插件非常方便地确保项目使用最新版本的依赖项,但最重要的是最新版本的依赖项:

https://www.mojohaus.org/versions-maven-plugin/ https://www.mojohaus.org/versions-maven-plugin/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM