gpt4 book ai didi

java - Java 8 和 JUnit 5 的 Maven 构建失败

转载 作者:行者123 更新时间:2023-12-01 17:03:07 25 4
gpt4 key购买 nike

我有一个使用 Java 8 的 Java 项目。我想使用 JUnit 5。我正在使用 Eclispe。如果我在项目的构建路径中添加 JUnit 5 库,则在 Eclipse 中构建会成功。但是,当我在命令行中使用 Maven 构建时,它失败了。

    Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: C:\Program Files\apache-maven-3.6.3\bin\..
Java version: 1.8.0_241, vendor: Oracle Corporation, runtime: C:\Program
Files\Java\jdk1.8.0_241\jre

在pom.xml下面。我尝试过在 Google 搜索中找到的其他版本的 pom,但都给了我同样的错误。更新:如果我删除 <scope>test</scope>从pom.xml来看,构建仍然失败。

<properties>
<!-- Dependency versions -->
<junit.jupiter.version>5.6.0</junit.jupiter.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<!-- Java 8 -->
<java.version>1.8</java.version>
<!-- Encoding -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- Jupiter API for writing tests -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Maven Surefire plugin to run tests -->
<build>
<plugins>
<!-- plugin to run test cases from maven -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
</plugin>
<!-- Maven plugin to use perticular java version to compile code -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
[INFO] 5 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.473 s
[INFO] Finished at: 2020-04-28T12:19:11-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project Java8: Compilation failure: Compilation failure:
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[8,23] error: package org.junit does not exist
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[8] error: static import only from classes and interfaces
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[31,2] error: cannot find symbol
[ERROR] symbol: method assertEquals(Optional<String>,Optional<Object>)
[ERROR] location: class ClassName
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[33,2] error: cannot find symbol
[ERROR] symbol: method assertEquals(Optional<Optional<String>>,Optional<Object>)
[ERROR] location: class ClassName
[ERROR] C:\Users\Documents\Java8\src\main\java\com\name\stream\ClassName.java:[44,2] error: cannot find symbol
[ERROR] symbol: method assertEquals(Optional<String>,Optional<Object>)
[ERROR] location: class ClassName
[ERROR] -> [Help 1]

最佳答案

Eclipse 无法正确分离测试范围和编译范围依赖项,因此请保持正确的范围测试。您必须在中创建一个测试类src/test/java/com/name/stream/ClassNameTest.java 并使用需要来自 org.junit.jupiter.api 包的适当注释

看看这个示例项目:github.com/khmarbaise/basic-junit-jupiter-test(简单的测试用例和断言也是带有单个方法的简单类,只是为了展示它是如何工作的..)...它在 IDEA Intellij 中的命令行上构建,也应该在 Eclipse 中工作,并在 GitHub Actions 上工作

关于java - Java 8 和 JUnit 5 的 Maven 构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61486003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com