[英]Test suite migration from JUnit4 to JUnit5
我已經將我項目中的所有 JUnit 測試從版本 4 遷移到版本 5。該項目由幾個 Maven 模塊和項目組成,如下面的架構
在 TestSuite.java 中,我在 Junit4 下有以下代碼
import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.extensions.cpsuite.ClasspathSuite.ClassnameFilters;
import org.junit.runner.RunWith;
@RunWith(ClasspathSuite.class)
@ClassnameFilters({"common.package.of.all.projects.*"})
public class TestSuite {
}
現在我嘗試在 JUnit5 下創建等效配置
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
@Suite
@SelectPackages("common.package.of.all.projects.*")
public class TestSuite {
}
這是測試套件模塊pom.xml的摘錄
// this lib help to execute the test suite under eclipse
<dependency>
<groupId>io.takari.junit</groupId>
<artifactId>takari-cpsuite</artifactId>
<scope>test</scope>
</dependency>
// these are the test dependencies I added
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-api</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<version>1.8.2</version>
<scope>test</scope>
</dependency>
// all module are in the dependencies like the following
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module.name</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
...
我使用 IntelliJ IDE 和 JDK 17.0.5,
但是當我嘗試運行測試套件時,沒有啟動任何測試。 盡管我在dependenciesToScan中添加了模塊,但似乎沒有找到
這是我收到的警告
WARNING: Third-party TestEngine implementations are forbidden to use the reserved 'junit-' prefix for their ID: 'junit-platform-suite'
Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-platform-suite' failed to discover tests
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discoverEngineRoot(EngineDiscoveryOrchestrator.java:111)
at org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.discover(EngineDiscoveryOrchestrator.java:85)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:92)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.NoSuchMethodError: 'org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.parentConfigurationParameters(org.junit.platform.engine.ConfigurationParameters)'
at org.junit.platform.suite.commons.SuiteLauncherDiscoveryRequestBuilder.build(SuiteLauncherDiscoveryRequestBuilder.java:198)
at org.junit.platform.suite.engine.SuiteTestDescriptor.discover(SuiteTestDescriptor.java:102)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
正如Spring Boot 2.2 關於此警告的另一篇錯誤“具有 ID 'junit-vintage' 的 TestEngine 未能發現測試”中所建議的,我在 spring-boot-starter-test 的父 pom 中添加了對 junit.vintage 的排除
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
所以我可能在我的配置中遺漏了一些東西,或者這不是實現我想要的東西的正確方法。
有任何想法嗎? 謝謝 !
您必須將junit-platform-suite-engine
和junit-jupiter-engine
作為依賴項。
它們都需要使用 JUnit Jupiter 運行套件。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.1</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.