기본 테스트 어노테이션
Junit5의 기본 어노테이션에대해 알아보겠습니다.
@Test
이 어노테이션을 붙이면 해당 메서드를 테스트 메서드로 인식합니다.
JUnit5 기준으로 접근제한자가 Default 여도 인식합니다. JUnit4 까지는 public이어야 했었습니다.
@Test
void create1() {
Study study = new Study();
assertNotNull(study);
System.out.println("create1()");
}
@Test
void create2() {
System.out.println("create2()");
}
@BeforeAll
이 어노테이션을 붙인 메서드는 테스트 클래스를 초기화할 때 최초로 딱 한번 수행되는 메서드입니다.
메서드 시그니쳐는 static 으로 선언해야합니다.
@BeforeAll
static void beforeAll() {
System.out.println("@BeforeAll");
}
@BeforeEach
이 어노테이션을 붙인 메서드는 모든 각각의 테스트 메서드가 실행될때마다 실행 이전에 한번씩 수행됩니다.
@BeforeEach
void beforeEach() {
System.out.println("@BeforeEach");
}
@AfterAll
이 어노테이션을 붙인 메서드는 해당 테스트 클래스 내 테스트 메서드를 모두 실행시킨 후 딱 한번 수행되는 메서드입니다.
메서드 시그니쳐는 static 으로 선언해야합니다.
@AfterAll
static void afterAll() {
System.out.println("@AfterAll");
}
@AfterEach
이 어노테이션을 붙인 메서드는 테스트 메서드 실행 이후에 수행됩니다.
@AfterEach
void afterEach() {
System.out.println("@AfterEach");
}
@Disabled
이 어노테이션을 붙인 테스트 메서드는 무시됩니다.
@Disabled
@Test
void create3() {
System.out.println("create3()");
}
코드
class StudyTest {
@Test
void create1() {
Study study = new Study();
assertNotNull(study);
System.out.println("create1()");
}
@Test
void create2() {
System.out.println("create2()");
}
@Disabled
@Test
void create3() {
System.out.println("create3()");
}
@BeforeAll
static void beforeAll() {
System.out.println("@BeforeAll");
}
@AfterAll
static void afterAll() {
System.out.println("@AfterAll");
}
@BeforeEach
void beforeEach() {
System.out.println("@BeforeEach");
}
@AfterEach
void afterEach() {
System.out.println("@AfterEach");
}
}
실행결과
@BeforeAll
@BeforeEach
create1()
@AfterEach
@BeforeEach
create2()
@AfterEach
@AfterAll
반응형
'웹 개발 > 🍃 SpringBoot' 카테고리의 다른 글
SpringBoot | POST,PUT,DELETE API를 작성하는 방법 (1) | 2024.12.26 |
---|---|
SpringBoot | GET API를 작성하는 방법 (0) | 2024.12.23 |
Project Metadata - spring initializr 정복하기(4) (1) | 2024.12.17 |
Spring Boot - spring initializr 정복하기(3) (0) | 2024.12.17 |
Language - spring initializr 정복하기(2) (0) | 2024.12.17 |