Spring Boot can control the execution of tests under certain conditions, unlike @Tag filtering by Tag.

First, Java environment

  • @EnabledOnJre: Specifies multiple JRE versions. The test can be performed only when the JRE version of the current test environment falls within this range.
  • @DisabledOnJre: Specifies multiple JRE versions. The test can be performed only when the JRE version of the current test environment is not included in this range.
  • @EnabledForJreRange: Specifies a JRE version range. Tests can be performed only if the JRE version of the current test environment is in this range.
  • @DisabledForJreRange: Specifies a JRE version range. The test can be performed only when the JRE version of the current test environment is not in the range.
package com.example.demo; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledForJreRange; import org.junit.jupiter.api.condition.DisabledOnJre; import org.junit.jupiter.api.condition.EnabledForJreRange; import org.junit.jupiter.api.condition.EnabledOnJre; import org.junit.jupiter.api.condition.JRE; public class UnitTest { @Test @EnabledOnJre({ JRE.JAVA_11, JRE.JAVA_17 }) public void enabledOnJava11And17Test() { System.out.println("Method[enabledOnJava11And17Test] executed."); } @Test @DisabledOnJre({ JRE.JAVA_17 }) public void disabledOnJava17Test() { System.out.println("Method[disabledOnJava17Test] executed."); } @Test @EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_17) public void enabledForJava8To17() { System.out.println("Method[enabledForJava8To17] executed."); } @Test @DisabledForJreRange(min = JRE.JAVA_11, max = JRE.JAVA_17) public void disabledForJava11To17() { System.out.println("Method[disabledForJava11To17] executed."); }}Copy the code

Second, operating system

  • EnabledOnOs: The test is performed when the current operating system is specified.
  • DisabledOnOs: The test is not performed when the current system is a specified operating system.
package com.example.demo; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; public class UnitTest { @Test @EnabledOnOs(OS.WINDOWS) public void enabledOnWindows() { System.out.println(" Run on Windows "); } @test@disableDonos (os.linux) public void disabledOnLinux() {system.out.println (" Not executed on LINUX ");} @test@disableDonos (os.linux) public void disabledOnLinux() {system.out.println (" not executed on LINUX "); }}Copy the code

3. System attributes

  • @EnabledIfSystemProperty: Tests are performed when the current system matches the specified system property name and expected value.
  • @DisabledIfSystemProperty: Tests are not performed when the current system matches specified system attribute names and expectations.
package com.example.demo; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIfSystemProperty; import org.junit.jupiter.api.condition.EnabledIfSystemProperty; public class UnitTest { @Test @EnabledIfSystemProperty(named = "user.language", Matches = "useful") public void enabledIfSystemPropertyUserLanguageIsZh () {System. Out. Println (" the current operating System user language is Chinese "); } @Test @DisabledIfSystemProperty(named = "os.arch", Matches = ".*64.*") public void disabledIfSystemProperty() {system.out.println (" 64-bit OS not executed "); }}Copy the code

4. Environmental variables

  • EnabledIfEnvironmentVariable: Tests are performed when the current system matches the specified environment variable name and expected value.
  • DisabledIfEnvironmentVariable: Tests are not performed when the current system matches specified environment variable names and expectations.
package com.example.demo; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; public class UnitTest { @Test @EnabledIfEnvironmentVariable(named = "JAVA_HOME", Matches = ". * ") public void enabledIfEnvironmentVariable () {System. Out. Println (" environment variable JAVA_HOME to exist to perform "); } @Test @DisabledIfEnvironmentVariable(named = "JAVA_HOME", 17 matches = ". *. *) "public void disabledIfEnvironmentVariable () {System. Out. Println (" environment variable JAVA_HOME there are 17 characters does not perform"); }}Copy the code

5. Customize conditions

JUnit versions later than 5.7 provide @enabledif and @disabledif annotations for user-defined test execution conditions. The @enabledif and @disabledif annotations accept a method name as an argument, and the method return value is of type Boolean.

package com.example.demo; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledIf; import org.junit.jupiter.api.condition.EnabledIf; public class UnitTest { @Test @EnabledIf("customCondition") public void enabledIf() { System.out.println("Method[enabledIf] executed."); } @Test @DisabledIf("customCondition") public void disabledIf() { System.out.println("Method[disabledIf] executed."); } private boolean customCondition() { return true; }}Copy the code

The EnabledIf and @disabledif annotations can be used not only on methods but also on classes. If used on classes, the methods specified by the @enabledif and @disabledif annotations must be static methods.

package com.example.demo; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIf; @EnabledIf("customCondition") public class UnitTest { @Test public void enabledIf() { System.out.println("Method[enabledIf] executed."); } private static boolean customCondition() { return true; }}Copy the code

If the @enabledif and @disabledif annotation parameters specify a method that is not in the same class as the test method, you must specify the method full path: package name. Class name # Method name.

package com.example.demo; public class CustomCondition { public static boolean customCondition() { return true; }}Copy the code
package com.example.demo; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIf; @EnabledIf("com.example.demo.CustomCondition#customCondition") public class UnitTest { @Test public void enabledIf() { System.out.println("Method[enabledIf] executed."); }}Copy the code