본문 바로가기

Web/spring

[Spring Test] JUnit 5 Conditional Test Execution with Annotations

728x90

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

 

 


 

 

 

조건부 테스트 적용 방법

 

Operating System Conditions

 

경우에 따라 OS(운영체제)에 따른 테스트 시나리오를 변경해야할 경우가 있다

@EnabledOnOs 애노테이션을 붙이고 값을 제공하면 된다. 배열 인수도 가능하다.

@Test
@EnabledOnOs({OS.WINDOWS, OS.MAC})
public void shouldRunBothWindowsAndMac() {
    //...
}

 

@DisabledOnOs 이름에서 알 수 있듯이 OS 유형 인수에 따라 테스트를 비활성화한다.

@Test
@DisabledOnOs(OS.LINUX)
public void shouldNotRunAtLinux() {
    //...
}

 

 

 

 

Java Runtime Environment Conditions

(Java 응용 프로그램을 실행하는 데 사용되는 소프트웨어 구성 요소의 번들)

@EnableOnJre  @DisableOnJre 애노테이션을 사용해서 JRE 특정 테스트가 가능하다

@Test
@EnabledOnJre({JRE.JAVA_10, JRE.JAVA_11})
public void shouldOnlyRunOnJava10And11() {
    //...
}

 

JUnit 5.6 부터는 @EnabledForJreRange로 특정 범위의 Java 버전에 대한 테스트를 활성화할 수 있다.

@Test
@EnabledForJreRange(min = JRE.JAVA_8, max = JRE.JAVA_13)
public void shouldOnlyRunOnJava8UntilJava13() {
    // this test will only run on Java 8, 9, 10, 11, 12, and 13.
}

기본적으로 최소값은 JAVA_8 이고 최대값은 가능한 최대 JRE 버전

특정 범위의 Java 버전에 대한 테스트를 비활성화하려면 @DisabledForJreRange를 쓴다

@Test
@DisabledForJreRange(min = JRE.JAVA_14, max = JRE.JAVA_15)
public void shouldNotBeRunOnJava14AndJava15() {
    // this won't run on Java 14 and 15.
}

 

8, 9, 10 및 11 이외의 Java 버전에서 실행되는 테스트를 비활성화하려면 JRE.OTHER enum 속성 을 사용할 수 있다

@Test
@DisabledOnJre(JRE.OTHER)
public void thisTestOnlyRunsWithUpToDateJREs() {
    // this test will only run on Java 8, 9, 10, and 11.
}

 

 

 

 

 

System Property Conditions

JVM 시스템 속성을 기반으로 테스트를 활성화하려면 @ EnabledIfSystemProperty 주석을 사용하면 된다

must provide named and matches arguments

named argument 는 정확한 시스템 속성을 지정하며

matches is used for defining the pattern of property value with a regular expression

 

Oracle virtual machine vendor 만 테스트할 경우

@Test
@EnabledIfSystemProperty(named = "java.vm.vendor", matches = "Oracle.*")
public void onlyIfVendorNameStartsWithOracle() {
    //...
}

 

 

@DisabledIfSystemProperty to disable tests based on JVM system properties

반대로 비활성화할때 쓴다

 

@Test
@DisabledIfSystemProperty(named = "file.separator", matches = "[/]")
public void disabledIfFileSeperatorIsSlash() {
    //...
}

 

 

 

 

Environment Variable Conditions

환경변수 조건

@EnabledIfEnvironmentVariable and @DisabledIfEnvironmentVariable annotations

 

like the annotations for system property conditions, these annotations take two arguments — named and matches

위와 마찬가지로 환경 변수 값과 일치시킬 환경 변수 이름 및 정규식을 지정하기 위해 두 개의 인수(  named 및 matches) 를 사용한다

 

@Test
@EnabledIfEnvironmentVariable(named = "GDMSESSION", matches = "ubuntu")
public void onlyRunOnUbuntuServer() {
    //...
}

@Test
@DisabledIfEnvironmentVariable(named = "LC_TIME", matches = ".*UTF-8.")
public void shouldNotRunWhenTimeIsNotUTF8() {
    //...
}

 


 

 

Creating Custom Conditional Annotations

기존 조건부 주석의 조합을 사용하여 사용자 지정 조건부 주석을 정의할 수 있다.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Test
@DisabledOnOs({OS.WINDOWS, OS.SOLARIS, OS.OTHER})
@EnabledOnJre({JRE.JAVA_9, JRE.JAVA_10, JRE.JAVA_11})
@interface ThisTestWillOnlyRunAtLinuxAndMacWithJava9Or10Or11 {
}

@ThisTestWillOnlyRunAtLinuxAndMacWithJava9Or10Or11
public void someSuperTestMethodHere() {
    // this method will run with Java9, 10, 11 and Linux or macOS.
}
728x90