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

Maven执行mvn test报错:package org.junit.jupiter.api不存在

问题描述

这是一个在Eclipse中开发的Java项目,使用Maven和JUnit5。已在pom.xml中配置相关依赖与构建信息,但执行mvn test时,仍提示org.junit.jupiter.api包不存在。本地已存在JUnit5相关文件夹,且pom中的版本与本地匹配,其他依赖无问题,仅API包报错,寻求技术帮助。

pom.xml配置内容
<dependencies>
    <!--  Junit5 --> 
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.7.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-runner</artifactId>
      <version>1.7.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.7.1</version>
      <scope>test</scope>
    </dependency>
    
  </dependencies>
  
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
    
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
        <dependencies>
            <dependency>
              <groupId>org.junit.platform</groupId>
              <artifactId>junit-platform-surefire-provider</artifactId>
              <version>1.2.0</version>
            </dependency>
        </dependencies>
        <configuration>
          <additionalClasspathElements>
            <additionalClasspathElement>src/test/java/</additionalClasspathElement>
          </additionalClasspathElements>
        </configuration>
      </plugin>
    
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <release>14</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
  
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.22.0</version>
      </plugin>
    </plugins>
  </reporting>
可能的解决方案
  • 清理本地Maven仓库损坏的依赖:删除本地仓库中org/junit/jupiter/junit-jupiter-api/5.7.1目录,执行mvn clean install -U强制更新依赖
  • 同步Eclipse的Maven依赖:右键项目 → Maven → Update Project,勾选"Force Update of Snapshots/Releases"后执行
  • 移除maven-surefire-plugin里的additionalClasspathElements配置:Maven默认会识别src/test/java为测试源码目录,手动添加可能引发类路径冲突
  • 升级maven-surefire-plugin至兼容JUnit Platform的版本(如3.0.0-M7及以上),同时移除junit-platform-surefire-provider依赖,新版本Surefire已内置JUnit 5支持

内容的提问来源于stack exchange,提问作者Lucas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 04:57:35