简体   繁体   English

使用 JUnitCore 时出现 java.lang.NoClassDefFoundError

[英]java.lang.NoClassDefFoundError in using JUnitCore

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

I found similar error in java.lang.NoClassDefFoundError in junit .在 junit 的 java.lang.NoClassDefFoundError 中发现了类似的错误。 But I already added junit to my project with maven .但是我已经使用mavenjunit添加到我的项目中。 Then, I checked Maven Dependencies and found there was junit-4.12.jar.然后,我检查了Maven Dependencies,发现有junit-4.12.jar。 I tried some different version of junit and spring , but all my attempts were failed.我尝试了一些不同版本的junitspring ,但我所有的尝试都失败了。 Below I attached My codes.下面我附上了我的代码。

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

Above is my pom.xml上面是我的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()));
    }
}

Surprisingly i was also facing same error log, adding these hamcrest-core solved the issue in my case.令人惊讶的是,我也面临相同的错误日志,添加这些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>

from Here .这里

When I changed junit to 4.7 with maven, it stil didn't work.当我使用 maven 将junit更改为 4.7 时,它仍然不起作用。 I checked Maven Dependencies and there was junit-4.7.jar .我检查了Maven Dependencies并且有junit-4.7.jar Still same error messeage.还是一样的错误信息。 I added junit-4.7.jar in Java Build Path again (See below image)我再次在Java Build Path添加了junit-4.7.jar (见下图)

在此处输入图片说明

Then, it worked well!然后,效果很好! But, still I don't know why this problem happened ;( . Does anyone know why?但是,我仍然不知道为什么会发生这个问题;(。有谁知道为什么?

I am even getting the same error as Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore我什至在线程“main” java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore 中遇到异常相同的错误

What I am basically trying here is to run the JUnit tests directly calling the executable jar file (of my application).我在这里基本上尝试的是运行 JUnit 测试,直接调用(我的应用程序的)可执行 jar 文件。 When I run the application in my local it works expected and main is called along with test execution happens当我在本地运行应用程序时,它按预期工作,并且 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);
    }

This calls the test class and runs all the desired tests but then when I try to execute the jar from the command line as below it fails C:\\Users\\tripate\\eclipse\\testing\\target>java -jar testing-0.0.1-SNAPSHOT.jar Hello World!这将调用测试类并运行所有所需的测试,但是当我尝试从命令行执行 jar 时,如下所示失败 C:\\Users\\tripate\\eclipse\\testing\\target>java -jar testing-0.0.1- SNAPSHOT.jar 世界,您好! Starting Integration Tests!开始集成测试!

    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)

Note: The jar is generated as the part of maven build here.注意:jar 是作为 maven build 的一部分在这里生成的。 I have both JUnit and hamcrest dependency in my POM file我的 POM 文件中有 JUnit 和 hamcrest 依赖项

you are running the test classes through test suite which run as java application not as test.您正在通过作为 Java 应用程序而不是作为测试运行的测试套件运行测试类。 In pom for junit dependency remove scope "test" , as scope is defined as test that's why test suite not able to find JUnit class.在 junit 依赖项的 pom 中删除范围 "test" ,因为范围被定义为测试,这就是测试套件无法找到 JUnit 类的原因。

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM