简体   繁体   中英

Junit 5 performing tests in maven build even when not annotated

While preparing a ( 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. ( release:prepare ) 上的发布时,我评论了一些我不想执行的测试,但它们无论如何都会执行,甚至完全从方法中删除@Test注释。

NOTE: Consider that the Skip Test option is ignored during maven release

The build was performed under Eclipse wit the :
- Apache Maven 3.5.4 external run-time (not the one embedded in Eclipse)
- Java JDK 1.8.0_151
- maven-compiler-plugin 3.0
- maven-release-plugin 2.5.3
- junit-jupiter-api 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.

UPDATE
I've also tried to use the @Disable annotation, unfortunately with no result. 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. 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. 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.

Try using @Ignore annotation.

@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. Normally the perform goal takes the class committed in your SCM during the release:prepare phase. 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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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