JUnit4와 JUnit5는 테스트 코드가 조금 다르다.
의존성 주입은 Tire인터페이스를 구현한 KoreaTire, AmericaTire클래스를 Car클래스에 주입하는데, xml파일에서 Car의 에 를 추가해 tire를 받게 했다.
xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="koreaTire" class="expert003.KoreaTire"></bean>
<bean id="americaTire" class="expert003.AmericaTire"></bean>
<bean id="car" class="expert003.Car">
<property name="tire" ref="koreaTire"></property>
</bean>
</beans>
그리고 JUnit으로 테스트를 해봤더니 org.junit.runners.model.InvalidTestClassError가 떴다.
왜 그런지 검색해보니 JUnit4와 JUnit5의 테스트 코드가 조금 달랐기 때문이다.
JUnit4
package expert003;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.jupiter.api.Assertions.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("expert003.xml")
public class CarTest {
@Autowired
Car car;
@Test
public void CarKoreaTireTest() {
assertEquals("tire name: korea tire", car.getTireBrand());
}
}
JUnit5는 다음과 같다.
@RunWith(SpringJUnit4ClassRunner.class)에서 @ExtendWith(SpringExtension.class)로 수정했다.
package expert003;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration("expert003.xml")
public class CarTest {
@Autowired
Car car;
@Test
public void CarKoreaTireTest() {
assertEquals("tire name: korea tire", car.getTireBrand());
}
}
SpringExtension을 못 찾는 오류가 발생한다면 springframework test 의존성을 추가하면 된다.
뤼튼의 도움을 받아 다음과 같이 작성했다.
pom.xml
<!-- Spring Test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.11</version> <!-- Match this version with your current spring version -->
<scope>test</scope>
</dependency>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version><!-- Use the latest version -->
<configuration>
<!-- Following line is important to run tests in older IDEs and builds systems like Jenkins-->
<useSystemClassLoader>false</useSystemClassLoader>
<!-- Following line is important to force IDEs run with JUnit Platform and not with older JUnit versions-->
<argLine>--add-modules=ALL-MODULE-PATH --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.base/java.util.concurrent=ALL-UNNAMED
</argLine>
</configuration>
<!-- Following lines are important for console output when tests are executed via Maven CLI-->
<dependencies >
<!--<dependency>-->
<!--<groupId>junit-platform-launcher-group-id></groupId>-->
<!--<artifactId>junit-platform-launcher-artifact-id></artifactId>-->
<!--<version>junit-platform-launcher-version></version>-->
<!--<!– scope not provided –>-->
<!--/dependency>-->
</dependencies>
</plugin>
'Springboot' 카테고리의 다른 글
스프링부트 테스트 시 리액트 앱 빌드 건너뛰기 (0) | 2024.02.03 |
---|---|
React & Springboot 통합 빌드시 Whitelabel Error 해결 (0) | 2024.01.24 |
[spring boot 3.1.4]spring security formLogin에서 로그인 안될때 (0) | 2023.10.10 |
intellij community버전에서 spring MVC 프로젝트 생성하기 (0) | 2023.09.19 |
spring에서 xml로 bean객체 등록할때 NoSuchBeanDefinitionException: No bean named 'id명' available (0) | 2023.09.19 |