使用JUnitCore时的java.lang.NoClassDefFoundError

时间:2018-02-03 16:35:39

标签: java spring eclipse maven junit

Exception in thread "main" java.lang.NoClassDefFoundError: 
org/junit/runner/JUnitCore
    at springbook.learningtest.junit.JUnitTest.main(JUnitTest.java:7)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

我在java.lang.NoClassDefFoundError in junit 中发现了类似的错误。但我已使用junitmaven添加到我的项目中。然后,我检查了Maven Dependencies,发现有junit-4.12.jar。 我尝试了一些不同版本的junitspring,但我的所有尝试都失败了。下面我附上了我的代码。

<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>TobySpring3.1</groupId>
    <artifactId>TobySpring3.1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source />
                    <target />
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.1.4.RELEASE</version>
        </dependency>
        <!-- JUNIT -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <!-- CGLIB -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>
    </dependencies>
</project>

以上是我的pom.xml

package springbook.learningtest.junit;

import org.junit.runner.JUnitCore;

public class JUnitTest {
    public static void main(String[] args) {
        JUnitCore.main("springbook.user.test.UserDaoTest");
    }
}

package springbook.user.test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.sql.SQLException;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import springbook.user.dao.UserDao;
import springbook.user.domain.User;

public class UserDaoTest {

    @Test
    public void addAndGet() throws ClassNotFoundException, SQLException {
        ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
        UserDao dao = context.getBean("userDao", UserDao.class);

        User user = new User();
        user.setId("gyumee");
        user.setName("name");
        user.setPassword("password");

        dao.add(user);

        User user2 = dao.get(user.getId());

        assertThat(user2.getName(), is(user.getName()));
        assertThat(user2.getPassword(), is(user.getPassword()));
    }
}

4 个答案:

答案 0 :(得分:1)

令人惊讶的是,我也遇到了同样的错误日志,添加这些hamcrest-core解决了我的问题。

<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

来自Here

答案 1 :(得分:1)

当我用maven将junit更改为4.7时,它仍无法正常工作。我检查了Maven Dependencies并且junit-4.7.jar。仍然是相同的错误消息。我再次在junit-4.7.jar中添加了Java Build Path(见下图)

enter image description here

然后,它运作良好!但是,我仍然不知道为什么会出现这个问题;(。有谁知道为什么?

答案 2 :(得分:1)

您正在通过测试套件运行测试类,这些测试套件作为Java应用程序而不是作为测试运行。 在pom for junit依赖项中,删除范围“ test” ,因为范围被定义为测试,这就是测试套件无法找到JUnit类的原因。

 <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
    </dependency>

答案 3 :(得分:0)

我什至收到与相同的错误 线程“ main”中的异常java.lang.NoClassDefFoundError:org / junit / runner / JUnitCore

我在这里基本上要尝试的是直接调用(我的应用程序的)可执行jar文件来运行JUnit测试。当我在本地运行该应用程序时,它可以正常工作,并且main与测试执行一起被调用

public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        System.out.println("Starting Integration Tests!");
        JUnitCore engine = new JUnitCore();
        engine.run(test.class);
    }

这将调用测试类并运行所有所需的测试,但是当我尝试从命令行执行jar时,如下所示,它将失败 C:\ Users \ tripate \ eclipse \ testing \ target> java -jar testing-0.0.1-SNAPSHOT.jar 你好,世界! 开始集成测试!

    Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
        at MVP2.testing.App.main(App.java:15)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

注意:jar是在这里作为Maven构建的一部分生成的。 我的POM文件中同时具有JUnit和hamcrest依赖项