如何解决Quarkus集成Specs2测试时Maven无法识别测试用例的问题
解决方案
- 移除测试类上的
@RunWith(classOf[JUnitRunner])注解,该注解是JUnit 4的专属注解,和你当前使用的JUnit 5环境冲突,会导致测试用例无法被识别。 - 修正maven-surefire-plugin配置,显式开启JUnit Platform支持并指定Scala测试类的匹配规则,修改后的插件配置参考如下:
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <environmentVariables> <training.topic>Weather</training.topic> </environmentVariables> <systemPropertyVariables> <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager> <maven.home>${maven.home}</maven.home> </systemPropertyVariables> <workingDirectory>${project.basedir}/target</workingDirectory> <!-- 新增测试类匹配规则 --> <includes> <include>**/*Test.scala</include> <include>**/*Spec.scala</include> </includes> </configuration> <!-- 新增JUnit Platform provider依赖 --> <dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-surefire-provider</artifactId> <version>1.8.1</version> </dependency> </dependencies> </plugin>
- 对齐字节码编译版本,你当前maven-compiler-plugin配置的JDK版本是11,但scala-maven-plugin指定的编译目标是JDK 8,会存在字节码兼容问题,修改scala-maven-plugin的编译参数:
<arg>-target:jvm-11</arg>
- 测试类需遵循以下写法规则:
- 类上仅添加
@QuarkusTest注解 - 测试类继承
org.specs2.mutable.Specification(或对应Specs2的Specification父类) - 测试方法添加
org.junit.jupiter.api.Test注解
示例代码:
- 类上仅添加
import io.quarkus.test.junit.QuarkusTest import org.junit.jupiter.api.Test import org.specs2.mutable.Specification @QuarkusTest class SampleQuarkusSpec extends Specification { @Test def testSampleLogic(): Unit = { 1 + 1 mustEqual 2 } }
- 若执行测试仍无法识别,可在pom的test依赖中额外添加JUnit Platform启动器依赖:
<dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.8.1</version> <scope>test</scope> </dependency>
内容的提问来源于stack exchange,提问作者user2102276
相关产品推荐
相关产品推荐

