0
votes

I am playing around with Junit 5 (conditional skip) and jdk-11.01 to ignore some tests for the first time. I do not manage to get the @Disable/@Enable to disable testcases. Please share any ideas how to get this to work?

Example:

@DisabledOnOs(OS.LINUX)
@EnableOnOs(OS.WINDOWS)
@DisabledForJreRange

https://www.baeldung.com/junit-5-conditional-test-execution

The only exclusion that still works are the @Ignore flag.

I have tested to run a test suite from "GitHub Actions" or locally from IntelliJ with the Junit 5 plugin, and the tests are still executed even though the condition is met (example running a test on Linux machine) when test is marked @EnableOnOs(OS.WINDOWS)!

Example test:

import org.junit.jupiter.api.condition.EnabledOnOs;
import org.junit.jupiter.api.condition.OS;
...
@EnabledOnOs(OS.WINDOWS)
@Test
public void onlyRunOnWindowsTest() {
    log.info("Test run on Windows env. only");
    // more test code ...
}

Is there anything else I need to configure the test with in order to have this working?

I also have this:

from dependencis in gradle.build I have: // https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.0'

in project External Libraries (intelliJ) I have:

> Gradle:junit:junit:4.1

I tried to remove this junit 4.1 (if there were due to conflict reasons), from project but this popped up again directly from a Gradle refresh. I guess junit5 uses this jar.

> Gradle:org.junit.jupiter:junit-jupiter-api:5.7.0
1
I can't reproduce the problem. Can you provide your build script? Along with a minimal reproducible example?Slaw
@Slaw did you do any special to not get this problem. Do you have booth Gradle:junit:junit:4.1 and Gradle:org.junit.jupiter:junit-jupiter-api:5.7.0 in the External Libraries for example?Daniel Nelson
I only had testImplementation("org.junit.jupiter:junit-jupiter:5.7.0") and test { useJUnitPlatform() }. If you're using JUnit 5 then you shouldn't have JUnit 4 unless you're using the former's vintage test engine.Slaw
Actually, I just fully realized that you're using @Ignore. That is a JUnit 4 annotation and does not exist in JUnit 5. The JUnit 5 equivalent is @Disabled. If @Ignore is working for you then my guess is you're running JUnit 4 and not JUnit 5.Slaw

1 Answers

0
votes

Yes, this is definitely a Junit 4 vs Junit 5 migration that went wrong. The following steps helped me get this to work.

If you are working with huge project this auto correction is most welcome (converting all tags and assertions to junit5 syntax automatically):

https://github.com/junit-pioneer/convert-junit4-to-junit5

Then you might also need to fix some project problems described in this post:

No tests found for given includes Error, when running Parameterized Unit test in Android Studio

Good luck!