java.lang.NoClassDefFoundError : org/hamcrest/Matchers

标签 java maven hamcrest

我已经使用 Maven 3 运行 JUnit5 测试用例。

现在我想在 Testcase AppTest.java 中的 Java 导入中使用库 hamcrest:

import static org.junit.jupiter.api.Assertions.*;

import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;

对于使用 App.java 的测试用例 AppTest.java:

double result;

result = app.probability_a_dash(23);
System.out.println("result: "+ result);

assertEquals(lessThan(0.5d), result);

导入时的错误是:

java.lang.NoClassDefFoundError: org/hamcrest/Matchers

如何将 hamcrest 库正确集成到 Maven 构建中: 命令行上的 mvn 正确集成了 hamcrest,只有 IDE intelliJ IDEA 会混淆导入!

不幸的是,JUnit5 没有检测到 0.0 < 0.5带 hamcrest lessThan :我如何获得通过的测试用例?

我的pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>

<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.auticon.birthdays</groupId>
  <artifactId>CalculateBirthdays</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>CalculateBirthdays</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <junit-platform.version>5.6.0</junit-platform.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-platform.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-platform.version}</version>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.auticon.birthdays.App</mainClass>
              </manifest>
            </archive>
          </configuration>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>




          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-failsafe-plugin</artifactId>
              <version>2.22.1</version>
              <configuration>
                  <argLine>
                      --illegal-access=permit
                  </argLine>
              </configuration>
          </plugin>

    <plugin>
                                <artifactId>maven-clean-plugin</artifactId>
                                <version>3.1.0</version>
                                <configuration>
                                        <filesets>
                                                <fileset>
                                                        <directory>${basedir}</directory>
                                                        <includes>
                                                                <include>**/contacts.xml</include>
                                                                <include>**/contacts (1).xml</include>
                                                                <include>**/contacts (2).xml</include>
                                                        </includes>
                                                        <excludes>
                                                                <exclude>**/important.log</exclude>
                                                                <exclude>**/another-important.log</exclude>
                                                        </excludes>
                                                        <followSymlinks>false</followSymlinks>
                                                </fileset>
                                        </filesets>
                                </configuration>
                        </plugin>


      </plugins>
    </pluginManagement>
  </build>
</project>

最佳答案

我在运行我的 Maven 项目时遇到了同样的错误。因此,我必须使用 hancrest-all,这是一个独立的 hamcrest jar,其中包含单个 Artifact 中的所有子模块。

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

检查此链接:https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all/1.3

关于java.lang.NoClassDefFoundError : org/hamcrest/Matchers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014906/

相关文章:

java - 方法调用转换

java - Swing,多次运行重复使用一个 "screen"

java - 添加两个 xml。使用 XSLT 变压器

java - IntelliJ - IntelliJ 运行配置忽略 maven.config 和 jvm.config 文件

java - 使用命令行和命令行中的不同 xml 执行 Maven/TestNG 项目?

java - 断言复杂对象时的 Junit 最佳实践

java - 如何使用 JUnit 和 Hamcrest 测试 .equals() 方法

java - 如何在android中将src设置为relativeLayout?

java - 使用 Maven 的 Spring Boot Web 应用程序不返回 HTML 页面

java - Hamcrest 匹配器使用项目的自定义匹配器按项目比较两个不同类型的集合