TDD - JUnit5

하우르·2021년 10월 25일

TDD

목록 보기
1/5

프로젝트 생성

spring boot에서 프로젝트를 생성 했다.
JUnit5은 2.2+ 버전의 스프링 부트 프로젝트를 만든다면 기본으로 JUnit 5 의존성 추가되어있다.

StudyTest 구현

package com.test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class StudyTest {

	@Test
	void create() {
		Study study = new Study();
		assertNotNull(study);
		System.out.println("@Test");
	}

	@Test
	@Disabled
	void create1() {
		Study study = new Study();
		assertNotNull(study);
		System.out.println("@Disabled");
	}

	@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");
	}


}

JUnit5 결과

개념

@Test
void create() {
	Study study = new Study();
	assertNotNull(study);
	System.out.println("@Test");
}

@Test

@Test가 붙여져 있는 메소드들은 테스트 대상임을 의미한다.

@BeforeAll

@Test가 붙여져 있는 메소드들을 실행 하기 전에 먼저 실행 된다.
이 메소드들은 static void를 사용 해야한다.

@AfterAll

@Test가 붙여져 있는 메소드들을 모두 실행되면 나중에 실행 된다.
이 메소드들은 static void를 사용 해야한다.

@BeforeEach

@Test가 붙여져 있는 메소드들이 실행하기 전 마다 먼저 실행된다.

@AfterEach

@Test가 붙여져 있는 메소드들이 실행후 종료될때 마다 실행된다.

@Disabled

@Disabled가 붙여져있는 메소드는 실행되지 않는다.

profile
주니어 개발자

0개의 댓글