Java Xiaohai 2022-01-27 00:11:45 阅读数:106
Spring Boot The test can be controlled to execute under certain conditions , Same as @Tag
Filter different by tag .
@EnabledOnJre
: Specify multiple JRE edition , Only the current test environment JRE The version is tested only in this range .@DisabledOnJre
: Specify multiple JRE edition , Only the current test environment JRE Testing is performed only when the version is not in this range .@EnabledForJreRange
: To specify a JRE Version range , Only the current test environment JRE The version is tested only in this range .@DisabledForJreRange
: To specify a JRE Version range , Only the current test environment JRE Testing is performed only when the version is not in this 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 code
EnabledOnOs
: Execute the test when the current system is the specified operating system .DisabledOnOs
: When the current system is the specified operating system, the test will not be executed .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("Windows Execute under the operating system ");
}
@Test
@DisabledOnOs(OS.LINUX)
public void disabledOnLinux() {
System.out.println("Linux Do not execute... Under the operating system ");
}
}
Copy code
@EnabledIfSystemProperty
: The test is executed when the current system matches the specified system attribute name and expected value .@DisabledIfSystemProperty
: The test is not executed when the current system matches the specified system attribute name and expected value .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 = "zh")
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 operating system does not execute ");
}
}
Copy code
EnabledIfEnvironmentVariable
: The test is executed when the current system matches the specified environment variable name and expected value .DisabledIfEnvironmentVariable
: The current system does not execute the test when it matches the specified environment variable name and expected value .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 Execute when present ");
}
@Test
@DisabledIfEnvironmentVariable(named = "JAVA_HOME", matches = ".*17.*")
public void disabledIfEnvironmentVariable() {
System.out.println(" environment variable JAVA_HOME There are characters 17 When does not perform ");
}
}
Copy code
JUnit 5.7 Later versions provide @EnabledIf
and @DisabledIf
Two annotations implement user-defined test execution conditions .@EnabledIf
and @DisabledIf
The annotation takes a method name as a parameter , Method return value type is 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 code
EnabledIf
and @DisabledIf
Annotations can be used not only for methods , It can also be used on classes , If it is used on a class , be @EnabledIf
and @DisabledIf
The method specified by the annotation parameter must be static .
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 code
If @EnabledIf
and @DisabledIf
The method specified by the annotation parameter is not in the same class as the test method , The full path of the method must be specified : Package name . Class name # Method name
.
package com.example.demo;
public class CustomCondition {
public static boolean customCondition() {
return true;
}
}
Copy 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.");
}
}
copyright:author[Java Xiaohai],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201270011420473.html