簡體   English   中英

在 Maven 中運行 JUnit 測試時出現 ClassNotFoundException

[英]ClassNotFoundException when running JUnit test in Maven

我目前正在嘗試為 Maven 項目設置自動化測試,但遇到了問題。 使用mvn test運行我的測試時,我得到以下結果:

-------------------------------------------------------------------------------
Test set: no.digipat.patornat.servlets.ServletTests
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.398 s <<< FAILURE! - in no.digipat.patornat.servlets.ServletTests
no.digipat.patornat.servlets.ServletTests  Time elapsed: 0.368 s  <<< ERROR!
java.lang.NoClassDefFoundError: com/mongodb/OperationExecutor
Caused by: java.lang.ClassNotFoundException: com.mongodb.OperationExecutor


在 Eclipse 中運行測試時出現同樣的錯誤。

這是我的 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>no.digipat.patornat</groupId>
  <artifactId>backend</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Pat or Nat Backend</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>11</maven.compiler.target>
    <maven.compiler.source>11</maven.compiler.source>
  </properties>

  <repositories>
    <repository>
      <id>cytomine-uliege-Cytomine-java-client</id>
      <url>https://packagecloud.io/cytomine-uliege/Cytomine-java-client/maven2</url>
      <releases>
        <enabled>true</enabled>
      </releases>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.lordofthejars</groupId>
      <artifactId>nosqlunit-mongodb</artifactId>
      <version>1.0.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.github.fakemongo</groupId>
      <artifactId>fongo</artifactId>
      <version>2.2.0-RC2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.github.stefanbirkner</groupId>
      <artifactId>system-rules</artifactId>
      <version>1.19.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>be.cytomine.client</groupId>
      <artifactId>cytomine-java-client</artifactId>
      <version>2.0.7-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.mongodb</groupId>
      <artifactId>mongo-java-driver</artifactId>
      <version>3.12.1</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M4</version>
        <configuration>
          <includes>
            <include>ServletTests.java</include>
          </includes>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

相關的Java文件:

ServletTests.java:

package no.digipat.patornat.servlets;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import static com.lordofthejars.nosqlunit.mongodb.InMemoryMongoDb.InMemoryMongoRuleBuilder.newInMemoryMongoDbRule;

@RunWith(Suite.class)
@SuiteClasses({MyTest.class})
public class ServletTests {

    private static final EnvironmentVariables environmentVariables = new EnvironmentVariables();
    @ClassRule
    public static final TestRule chain = RuleChain
        .outerRule(newInMemoryMongoDbRule().build())
        .around(environmentVariables);

    @BeforeClass
    public static void setUpClass() {
        environmentVariables.set("MY_VARIABLE", "some value");
    }

}

我的測試.java:

package no.digipat.patornat.servlets;
import static org.junit.Assert.*;

import org.junit.Test;

public class MyTest {

    @Test
    public void test() {
        fail("Not yet implemented");
    }

}

關於如何解決這個問題的任何想法? 我嘗試運行mvn clean (以前幫助我解決了類似的問題),但無濟於事。

我認為你的pom.xml需要maven-compiler-plugin

<build>
  <!-- put here the path of your test source directory -->
  <testSourceDirectory>src/test</testSourceDirectory>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>${maven.compiler.source}</source>
        <target>${maven.compiler.target}</target>
      </configuration>
    </plugin>
  </plugins>
  ...

問題似乎是最新版本的 Fongo 和 mongo-java-driver 不兼容。 當我將 Fongo 和/或 Mongo Java 驅動程序的依賴項更改為舊版本(我分別專門嘗試了 2.1.0 和 3.6.3 版本)時,錯誤消失了。 然而,由於這個解決方案似乎相當脆弱和不靈活,最好的辦法可能是改用 Fongo 的替代方案。 根據這個 GitHub 評論mongo-java-server可能是一個不錯的選擇。 一種可能更強大的替代方案,也是我最終可能會使用的替代方案,是使用“真實”的測試數據庫。 本文提供了有關執行此操作的幾種方法的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM