java .lang.NoClassDefFoundError: org / hamcrest / Matchers - PullRequest
0 голосов
/ 01 февраля 2020

У меня запущены тестовые случаи JUnit5 с Maven 3.

Теперь я хочу использовать библиотеку hamcrest в Java импорте в Testcase AppTest. java:

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

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

Для Testcase AppTest. java, использующий приложение. 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 с помощью подколенного сухожилия 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>

1 Ответ

0 голосов
/ 01 февраля 2020

Вместо JUnit5 assertEquals мне пришлось использовать в AppTest.java следующие hamcrest методы:

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

и

assertThat(result, lessThan(0.5d));
...