Backend/TDD, Junit

SpringBoot 1.5.x 에서 Junit5 사용하기 (with Maven)

findmypiece 2021. 7. 20. 23:45
728x90

1. 사용할 junit-jupiter, junit-platform 버전 properties 로 지정

 - 이건 필수는 아니다. 그냥 버전을 전역적으로 관리하기 위해...

<properties>
	<junit-jupiter.version>5.7.1</junit-jupiter.version>
</properties>

 

2. pom.xml 에서 SpringBoot 1.5.x 에 포함된 Junit4 의존성 제외처리

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 <exclusions>
     <exclusion>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
     </exclusion>
 </exclusions>
</dependency>

 

3. junit-jupiter-api, junit-jupiter-engine 의존성 추가

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-api</artifactId>
  <version>${junit-jupiter.version}</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-engine</artifactId>
  <version>${junit-jupiter.version}</version>
  <scope>test</scope>
</dependency>

 

4. spring-test-junit5 의존성 추가

 - 이게 있어야 테스트 클래스에 @ExtendWith(SpringExtension.class) 를 사용할 수 있다.

    참고로 이는 Junit5 환경 테스트 코드에서 Spring 컨텍스트를 사용하기 위해서는 반드시 필요하다.

    SpringBoot 2.1 이상부터는 @SpringBootTest 에 포함되었지만 이전버전에서는 명시적으로 지정해줘야 한다.   

<dependency>
	<groupId>com.github.sbrannen</groupId>
	<artifactId>spring-test-junit5</artifactId>
	<version>1.5.0</version>
	<scope>test</scope>
</dependency>

 

5. 플러그인 추가

 - 반드시 2.22.2 버전이상으로 설정해야 함.

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.22.2</version>
</plugin>
<plugin>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.22.2</version>
</plugin>

 

6. 테스트 코드 작성

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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class Test {

	@Test
	public void test(){
		assertEquals(true, true);
	}

}

 

728x90

'Backend > TDD, Junit' 카테고리의 다른 글

Mockup 서버는 무엇을 사용해야 하나?  (0) 2021.07.22
테스트 구성파일 관리  (0) 2021.07.21