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

Maven执行mvn clean verify时报org.junit包不存在,但Eclipse中测试运行正常的问题求助

Maven执行mvn clean verify时报org.junit包不存在,但Eclipse中测试运行正常的问题求助

各位大佬,我最近碰到一个特别头疼的问题,想请大家帮忙分析下:

我的项目在Eclipse里运行测试完全正常,但一旦用Maven命令行执行就出问题:

  • 如果先让Eclipse编译好项目,再执行mvn verify,看起来每个测试类都被扫描到了,但每个类里都跑0个测试;
  • 要是执行mvn clean verify,直接就编译失败了,报JUnit包找不到的错误,具体错误日志如下:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project jcivitas: Compilation failure: Compilation failure:
[ERROR] /home/mag/project/civitas-0.7.1/jcivitas/src/test/java/civitas/common/util/ReadSimpleIntTagTest.java:[3,24] package org.junit does not exist
[ERROR] /home/mag/project/civitas-0.7.1/jcivitas/src/test/java/civitas/common/util/ReadSimpleIntTagTest.java:[3,1] static import only from classes and interfaces

我已经排查了很多方向:

  • 确认测试类都放在src/test/java目录下,路径完全合规;
  • pom里已经引入了JUnit 5的依赖,测试类里用的都是org.junit.jupiter.api下的注解和断言,根本没用到旧的JUnit 4的org.junit包;
  • 删掉过本地的~/.m2仓库,也在Eclipse里反复执行了Maven同步操作;
  • 之前项目是完全正常的(已经写了99个测试用例),后来改了pom里的一些配置(早就把那些改动删掉了)之后就出问题了;
  • 试过网上能找到的各种JUnit 5依赖组合,包括官方文档里的标准配置;
  • 项目必须兼容Java 8,当前系统用的是Temurin JDK 1.8.0_432-b06。

下面是我当前的pom.xml配置:

<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>com.kodekonveyor</groupId>
  <artifactId>jcivitas</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>jcivitas</name>
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.13.0-M3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.13.0-M3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>4.0.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

报错的测试类开头代码如下:

package civitas.common.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import civitas.common.Util;

public class ReadSimpleTagTest extends UtilTestBase {

实在找不到问题根源了,有没有大佬能给点思路?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 11:23:02