-
Junit5 에서 Junit4로 변경하기프레임워크/Spring Boot 2023. 12. 24. 14:44
1. 의존성 변경
스프링에서 junit 테스트를 실행하기 위해서는 아래와 같은 의존성을 추가해서 사용합니다.
testImplementation 'org.springframework.boot:spring-boot-starter-test'
위 의존성은 Junit5를 기준으로 작동하게 되어있기 때문에 Junit4를 사용하고 싶으면 수정이 필요합니다.
spring-boot-starter-test는 여러가지 패키지들을 조합해서 사용하는데, 대표 적으로 Junit5와 관련된 패키지는 아래와 같습니다.
- junit-jupiter
- junit-platform-commons
따라서 Junit4를 사용하고 싶다면 위의 패키지들을 제외하고, 추가로 Junit4를 의존성에 추가해주면 사용가능합니다.
testImplementation 'junit:junit:4.13.2' // JUnit 4 추가 testImplementation ('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.jupiter', module: 'junit-jupiter' exclude group: 'org.junit.platform', module: 'junit-platform-commons' }
2. 코드변경
Junit4에서는 테스트 클래스와 메소드가 public으로 설정되어야 합니다.
기존 Junit5에서 import해서 사용하던 Test 어노테이션이 있다면 4버전으로 변경이 필요합니다.
@RunWith(SpringRunner.class) public class RecommendationServiceApplicationTests { ... }
[이미지 출처]
https://junit.org/junit4/ (글 대표 이미지)
'프레임워크 > Spring Boot' 카테고리의 다른 글
데이터베이스를 기반으로 JPA의 연관관계 살펴보기 <1> (0) 2024.02.06 Entity의 Id 생성 전략에 따른 EntityManager의 persist 동작 확인 (0) 2024.02.06 Spring boot 2.7.18에서 swagger 설정하기 <2> (0) 2024.01.23 Spring boot 2.7.18에서 swagger 설정하기 <1> (0) 2024.01.22 mongodb의 @Indexed 가 작동하지 않을 때 (0) 2023.12.24