HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
📝
남득윤 학습 저장소
/
더 자바, 애플리케이션을 테스트하는 다양한 방법
더 자바, 애플리케이션을 테스트하는 다양한 방법
/
1부 - JUnit5
1부 - JUnit5
/
JUnit 5 조건에 따라 테스트 실행하기

JUnit 5 조건에 따라 테스트 실행하기

 
Assumptions.assumeTrue
 
@Test void TEST_ENV_환경변수가_LOCAL_일때만_실행(){ Assumptions.assumeTrue("LOCAL".equalsIgnoreCase(System.getenv("TEST_ENV"))); Study study = new Study(); assertThat(study).isNotNull(); }
 
조건이 틀렸다면 test는 ignored됨
 
참고) 환경변수를 바꾸면 인텔리제이를 다시 띄워야함
 
Assumptions.asumingThat
@Test void testInAllEnvironments() { Assumptions.assumingThat("CI".equals(System.getenv("ENV")), () -> { // perform these assertions only on the CI server assertEquals(2, calculator.divide(4, 2)); }); // perform these assertions in all environments assertEquals(42, calculator.multiply(6, 7)); }
Env 환경변수가 CI인 경우에만 추가로 4/2=2를 테스트함
 
@EnabledOnOs, @DisabledOnOs
@EnabledOnJre, @DisabledOnJre
@EnabledOnIfSystemProperty, @DisabledOnIfSystemProperty
@EnabledOnIfEnvironmentVariable, @DisabledIfEnvironmentVariable
 
 
사용 예시 및 커스텀 Annotation - @TestOnMac
@Test @EnabledOnOs(MAC) void onlyOnMacOs() { // ... } @TestOnMac void testOnMac() { // ... } @Test @EnabledOnOs({ LINUX, MAC }) void onLinuxOrMac() { // ... } @Test @DisabledOnOs(WINDOWS) void notOnWindows() { // ... } @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Test @EnabledOnOs(MAC) @interface TestOnMac { }