在Maven中运行JUnit测试时发生ClassNotFoundException

时间:2020-02-14 08:51:34

标签: java maven junit

我目前正在尝试为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");
    }

}

MyTest.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(以前帮助我解决了类似问题),但无济于事。

2 个答案:

答案 0 :(得分:0)

我认为您需要maven-compiler-plugin中的pom.xml

<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>
  ...

答案 1 :(得分:0)

问题似乎是Fongo和mongo-java-driver的最新版本不兼容。当我将Fongo和/或Mongo Java驱动程序的依赖项更改为较旧的版本时(分别尝试了2.1.0和3.6.3版),错误消失了。但是,由于该解决方案似乎相当脆弱且不灵活,因此最好的选择可能是切换到Fongo的替代方案。根据{{​​3}},this GitHub comment可能是一个不错的选择。我可能最终会使用的一个更健壮的替代方法是使用“真实”测试数据库。 mongo-java-server拥有有关几种方法的信息。