简体   繁体   English

java.lang.NoClassDefFoundError: org/hamcrest/Matchers

[英]java.lang.NoClassDefFoundError: org/hamcrest/Matchers

I have running JUnit5 testcases w/ Maven 3.我已经使用 Maven 3 运行 JUnit5 测试用例。

Now I want to use library hamcrest in Java import in Testcase AppTest.java:现在我想在 Testcase AppTest.java 的 Java 导入中使用库 hamcrest:

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

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

For Testcase AppTest.java that uses App.java:对于使用 App.java 的测试用例 AppTest.java:

double result;

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

assertEquals(lessThan(0.5d), result);

The error on the import is:导入的错误是:

java.lang.NoClassDefFoundError: org/hamcrest/Matchers java.lang.NoClassDefFoundError: org/hamcrest/Matchers

How do I get hamcrest library properly integrated in maven build: mvn on the command line integrates hamcrest properly, only IDE intelliJ IDEA is mixing up the imports!如何在 maven build 中正确集成 hamcrest 库:命令行上的 mvn 正确集成了 hamcrest,只有 IDE intelliJ IDEA 混淆了导入!

Unfortunately JUnit5 does not detect that 0.0 < 0.5 with hamcrest lessThan : How do I get a passing testcase on this?不幸的是,JUnit5 没有检测到0.0 < 0.5与 hamcrest lessThan :我如何获得通过的测试用例?

My pom.xml is as follows:我的 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>

Instead of JUnit5 assertEquals , I had to use in AppTest.java the following hamcrest methods:我不得不在AppTest.java使用以下hamcrest方法,而不是 JUnit5 assertEquals

import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

and

assertThat(result, lessThan(0.5d));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”中的异常 java.lang.NoClassDefFoundError: org/hamcrest/Matchers - Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/Matchers java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing - java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing IntelliJ java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing - IntelliJ java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing “线程”main“中的异常 java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing” - “Exception in thread ”main“ java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing” JUnit抛出java.lang.NoClassDefFoundError:org / hamcrest / MatcherAssert - JUnit throws java.lang.NoClassDefFoundError: org/hamcrest/MatcherAssert java.lang.NoClassDefFoundError:使用Maven的org / hamcrest / SelfDescribing - java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing using Maven java.lang.NoClassDefFoundError:Intellij中的org / hamcrest / SelfDescribing - java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing in Intellij NoClassDefFoundError在org / hamcrest / Matchers在Junit上 - NoClassDefFoundError on org/hamcrest/Matchers on junit Eclipse中的Junit测试失败,出现java.lang.NoClassDefFoundError:org / hamcrest / SelfDescribing - Junit tests in Eclipse are failing with java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing 使用Ant构建带有初始化错误的junit测试:java.lang.NoClassDefFoundError:org / hamcrest / SelfDescribing - Use Ant to build junit test with initializationError: java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM