本篇介紹在Spring Boot 2.1專案中整合JUnit 5。
記帳簿專案使用的Spring Boot版本為2.1.3.RELEASE
,而spring-boot-starter-test
預設依賴的JUnit版本為4.12
。
注意,從Spring Boot 2.2版本開始spring-boot-starter-test
預設已經是JUnit 5,就無需再進行以下設定。請參考Spring Boot 2.2 Release Notes
你可以在Eclipse打開專案的pom.xml
,點選[Dependency Hierarchy]標籤來檢視所有的依賴,從中可以看到spring-boot-starter-test
下對JUnit的依賴版本。
因此若要整合JUnit 5,則要在pom.xml
加入以下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- exclude JUnit 4 -->
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
上面設定分別在spring-boot-starter-test
中加入對JUnit的排除設定,也就是<exclusion>
的部分;及加入JUnit 5的依賴。
以下為目前pom.xml
的全部配置。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>idv.matt</groupId>
<artifactId>moneynote</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>moneynote</name>
<description>moneynote</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- exclude JUnit 4 -->
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>idv/matt/mapper/*.xml</include>
</includes>
</resource>
</resources>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
修改pom.xml
後存檔,接著會發現原本test程式中JUnit相關的import都出現...cannot be resolved to a type
的紅叉叉錯誤,這就是因為上面已將JUnit 4的library排除了,必須要改用JUnit 5的api。
要修改的地方分別為:
- 把
@Test
的import 由JUnit 4的org.junit.Test
改為JUnit 5的org.junit.jupiter.api.Test
- 把JUnt 4的
@Ignore
改為@Disabled
- 把JUnit 4的
org.junit.Assert
改為JUnit 5的org.junit.jupiter.api.Assertions
。 - 移除測試類別上的
@RunWith(SpringRunner.class)
。因為Sprinb Boot執行JUnit 5要改用@ExtendWith(SpringExtension.class)
,而@SpringBootTest
中已經有整合了。
其他需要調整的地方請參考JUnit 4 Migration Tips
以使用SpringBoot打造記帳簿專案(十三)撰寫第一支測試程式寫過的HelloControllerTest
為例,修改後如下:
HelloControllerTest
package idv.matt.controller;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import idv.matt.application.MoneynoteApplication;
@SpringBootTest(classes = { MoneynoteApplication.class })
public class HelloControllerTest {
@Autowired
HelloController helloController;
@Test
public void testHelloReturnExpectedString() {
final String expected = "hello moneynote";
final String actual = helloController.hello();
Assertions.assertEquals(expected, actual);
}
@Test
public void testHelloReturnUnexceptedString() {
final String expected = "hello";
final String actual = helloController.hello();
Assertions.assertNotEquals(expected, actual);
}
}
修改完以上後,先在專案上按滑鼠右鍵 -> Run As -> Run Configurations...
,找到JUnit選項並清空其中的test執行記錄後,才重新執行測試程式。這是因為先前的執行紀錄仍是依賴JUnit 4版本,若再執行同一支測試程式,可能會用先前的執行紀錄中的設定來執行而出現找不到JUnit 4的錯誤。
接者請看使用SpringBoot打造記帳簿專案(二十四)註冊Controller
參考:
沒有留言:
張貼留言