Bepoz
파즈의 공부 일기
Bepoz
전체 방문자
오늘
어제
  • 분류 전체보기 (232)
    • 공부 기록들 (85)
      • 우테코 (17)
    • Spring (85)
    • Java (43)
    • Infra (17)
    • 책 정리 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Bepoz

파즈의 공부 일기

Spring

[Spring] @BeforeEach @BeforeAll @AfterEach @AfterAll 에 대해

2020. 12. 2. 02:48

@BeforeEach @BeforeAll @AfterEach @AfterAll 에 대해

테스트를 실행 시에 반복되는 세팅 로직 등이 있을 때에 이 어노테이션들을 사용할 수가 있다. 코드로 바로 확인해보자

@SpringBootTest
public class eachAllTest {

    @BeforeEach
    public void beforeEach() {
        System.out.println("BeforeEach");
    }

    @BeforeAll
    static void beforeAll(){
        System.out.println("BeforeAll");
    }

    @AfterEach
    public void afterEach() {
        System.out.println("AfterEach");
    }

    @AfterAll
    static void afterAll() {
        System.out.println("AfterAll");
    }

    @Test
    public void test1() throws Exception{
        System.out.println("test1");
    }

    @Test
    public void test2() throws Exception{
        System.out.println("test2");
    }

특이한 점은 ~All 어노테이션이 붙은 메서드들은 static이 붙었다는 것이다.

실제로 실행시점도 static이 참조될 때 인지 스프링 로고가 뜨기전에 BeforeAll 가 print 되는 것을 알 수가 있었다.

BeforeEach
test1
AfterEach

Each는 각 테스트들 마다 적용이 된다. 말그대로 each 각각 인 셈이다.

  1. 전체 테스트를 돌렸을 때
BeforeAll

BeforeEach
test1
AfterEach

BeforeEach
test2
AfterEach

AfterAll
  1. 1개의 테스트만 돌렸을 때
BeforeAll

BeforeEach
test1
AfterEach

AfterAll

전체 테스트 돌렸을 때에만 All 이 호출되는 것은 아닐까? 라고 생각을 해봤지만 그것은 아니었다.
이 어노테이션들은 테스트 코드 운영 시에 굉장히 유요하다는 것을 사용해보면 알 수 있을 것이다.

굳이 주의할 점을 뽑는다면, @BeforeEach로 특정 엔티티를 생성해주고 save 시켰는데 @AfterEach정의하는 것을 놓쳐서 그것을 삭제시켜주지 못해서 @BeforeEach 만 여러번 호출이 되어 문제가 생길 수 있다는 점을 유의해야 할 것이다.


Reference

BaelDung Docs

'Spring' 카테고리의 다른 글

[Spring] Spring Validation 처리에 대해  (0) 2021.05.09
[Spring] @Transactional 에 대해  (0) 2021.05.09
[Spring] Validator 생성 시 주의해야 할 점, Invalid target 오류  (1) 2020.12.02
[Spring] @NotNull, @NotEmpty, @NotBlank 에 대해  (0) 2020.11.17
[Spring] @Builder에 대해  (0) 2020.11.16
    'Spring' 카테고리의 다른 글
    • [Spring] Spring Validation 처리에 대해
    • [Spring] @Transactional 에 대해
    • [Spring] Validator 생성 시 주의해야 할 점, Invalid target 오류
    • [Spring] @NotNull, @NotEmpty, @NotBlank 에 대해
    Bepoz
    Bepoz
    https://github.com/Be-poz/TIL

    티스토리툴바