JUnit 5 cung cấp a way out of the box.
JUnit 5 = JUnit vBulletin + JUnit Jupiter + JUnit Vintage
Mỗi người là một dự án riêng biệt và sử dụng tất cả trong số họ cho phép để biên dịch và thực hiện JUnit 4 và JUnit 5 xét nghiệm trong một dự án tương tự .
JUnit Jupiter là sự kết hợp của mô hình lập trình và mở rộng mô hình mới cho các văn bản kiểm tra và mở rộng trong JUnit 5.
JUnit Vintage cung cấp một TestEngine để chạy JUnit 3 và JUnit 4 dựa kiểm tra trên nền tảng.
Các JUnit vBulletin phục vụ như một nền tảng cho việc tung ra các khuôn khổ thử nghiệm trên JVM
Dưới đây là cấu hình tối thiểu để sử dụng với Maven để cấu hình dự án để biên dịch và chạy cả Junit4 và JUnit5 kiểm tra:
<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>
<groupId>mygroup</groupId>
<artifactId>minimal-conf-junit4-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- JUnit 5 depends on JDK 1.8 -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- JUnit dependency versions -->
<junit.version>4.12</junit.version>
<junit-vintage-engine>4.12.1</junit-vintage-engine>
<junit-jupiter.version>5.0.1</junit-jupiter.version>
<junit-platform.version>1.0.1</junit-platform.version>
</properties>
<dependencies>
<!--JUnit Jupiter API to write and compile tests with JUnit5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<!-- JUnit 4 to make legacy JUnit 4 tests compile -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version> <!-- matters until now-->
<dependencies>
<!-- to let surefire to run JUnit 4 but also JUnit 5 tests -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit-platform.version}</version>
</dependency>
<!-- JUnit vintage engine to run JUnit 3 or JUnit 4 tests -->
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-vintage-engine}</version>
</dependency>
<!-- JUnit 5 engine to run JUnit 5 tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Bây giờ mvn test
biên dịch và chạy cả kiểm tra JUnit 4 và JUnit 5.
Lưu ý 1: junit-vintage-engine
(4.12.1
) và junit
(4.12
) phụ thuộc không chỉ định chính xác phiên bản tương tự.
Đây không phải là một vấn đề ở tất cả như:
Note 2: maven-surefire-plugin với 2.19.1
vấn đề phiên bản bất cứ điều gì bạn muốn sử dụng JUnit5 một mình hoặc cả hai Junit4 và JUnit5.
Phiên bản tiếp theo của plugin thực sự là một số ngoại lệ trong quá trình thực hiện kiểm tra JUnit 5.
The issue khá cũ nhưng chưa được giải quyết.
Bạn cũng sẽ cần 'junit-jupiter-engine'. –
Cảm ơn rất nhiều Marc!Tôi đã thử nghiệm với một dự án khởi động Spring chắc chắn đã thêm nó cho tôi. Tôi đã cập nhật. – davidxxx