You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决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>
  • 测试类需遵循以下写法规则:
    1. 类上仅添加@QuarkusTest注解
    2. 测试类继承org.specs2.mutable.Specification(或对应Specs2的Specification父类)
    3. 测试方法添加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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 15:36:04