首页 > 解决方案 > JUnit 测试用例抛出 NoClassDefFoundError

问题描述

我正在尝试执行基本测试用例,我已经完成了设置,但出现以下错误:

java.lang.NoClassDefFoundError: org/junit/platform/engine/EngineDiscoveryListener
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.getLauncherDiscoveryListener(LauncherDiscoveryRequestBuilder.java:241)
    at org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.build(LauncherDiscoveryRequestBuilder.java:235)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createUnfilteredTest(JUnit5TestLoader.java:75)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.createTest(JUnit5TestLoader.java:66)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestLoader.loadTests(JUnit5TestLoader.java:53)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:526)

在我的ConfigServer班级下面找到:

package org.siaa.devops;

import java.util.Hashtable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.config.server.EnableConfigServer;

import com.jcraft.jsch.JSch;

@SpringBootApplication
@EnableConfigServer
public class ConfigServer extends SpringBootServletInitializer {

    private static final String PREFERRED_AUTENTICATION_MODE = "publickey";

    public static void main(String[] args) {
        // Hack in JSCH to avoid interactive session
        // Override PreferredAuthentications in JSCH
        Hashtable<String, String> config = new Hashtable<String, String>();
        config.put("PreferredAuthentications", PREFERRED_AUTENTICATION_MODE);
        JSch.setConfig(config);

        SpringApplication.run(ConfigServer.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder application) {
        return application.sources(ConfigServer.class);
    }

}

构建.gradle

group 'org.siaa.develop'

buildscript {
    ext {
        //springBootVersion = '1.5.4.RELEASE'
        //springBootVersion = '2.0.6.RELEASE'
        springBootVersion = '2.2.6.RELEASE'
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}


apply plugin: "io.spring.dependency-management"
apply plugin: 'org.springframework.boot'
apply plugin: 'java'
apply plugin: 'eclipse'

//version = '0.0.1-SNAPSHOT'

//springBoot {
  //executable = true
//}

bootJar {
    launchScript()
}

ext {
    springCloudVersion = 'Hoxton.SR3'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    //mavenCentral()
    maven {
        url 'http://artifactory.siaa.org/artifactory/siaa-mp-develop'
    }
}


dependencies {
    compile('org.springframework.cloud:spring-cloud-config-server:2.2.2.RELEASE')
    compile('org.springframework.boot:spring-boot-starter-actuator:2.2.6.RELEASE')
    compile('org.springframework.cloud:spring-cloud-security:2.2.1.RELEASE')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    
    //Kafka and spring cloud config monitor
    compile('org.springframework.cloud:spring-cloud-starter-bus-kafka:2.0.0.RELEASE')
    compile('org.springframework.cloud:spring-cloud-config-monitor:2.2.1.RELEASE')
    
    //Prometheus
    compile('io.micrometer:micrometer-registry-prometheus')
    implementation("org.junit.jupiter:junit-jupiter:5.6.0")

}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

添加的测试用例是ConfigServerTest

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ConfigServerTest {

    @Test
    public void contextLoads() {
    }

}

我已经进行了研究,并且相同的代码似乎在其他机器上运行良好,但在我的本地引起了问题。无法修复它。请帮帮我。

我使用的 Gradle 版本是:Gradle-6.5.1 Java (JRE)version :1.8.0_40

提前致谢。

标签: javaspring-bootgradlejunit

解决方案


推荐阅读