当前位置: 首页 > 知识库问答 >
问题:

java.lang.NoClassDefFoundError:org/junit/jupiter/api/扩展/脚本评估异常

史钊
2023-03-14

我想在我的sprint boot应用程序中添加端到端的测试,并遵循这个教程:https://reflectoring.io/spring-boot-web-controller-test/

package com.example.petstore.backend.api.implementation;

import com.example.petstore.backend.api.model.Pet;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = PetController.class)
public class PetControllerE2E {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;
    
    @Test
    void whenValidInput_thenReturns200() throws Exception {
        Pet pet = new Pet().id(0L);

        mockMvc.perform(post("/pet")
            .contentType("application/json")
            .content(objectMapper.writeValueAsString(pet)))
            .andExpect(status().isOk());
    }

}

如果我运行这个测试,我会得到:

org/junit/jupiter/api/extension/ScriptEvaluationException
java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/ScriptEvaluationException
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:315)
    at org.junit.jupiter.engine.extension.ScriptExecutionCondition$Evaluator.forName(ScriptExecutionCondition.java:153)
    at org.junit.jupiter.engine.extension.ScriptExecutionCondition$Evaluator.forName(ScriptExecutionCondition.java:133)
    at org.junit.jupiter.engine.extension.ScriptExecutionCondition.<init>(ScriptExecutionCondition.java:55)
    at org.junit.jupiter.engine.extension.ScriptExecutionCondition.<init>(ScriptExecutionCondition.java:50)
    at org.junit.jupiter.engine.extension.ExtensionRegistry.<clinit>(ExtensionRegistry.java:54)
    at org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor.prepare(JupiterEngineDescriptor.java:37)
    at org.junit.jupiter.engine.descriptor.JupiterEngineDescriptor.prepare(JupiterEngineDescriptor.java:28)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$1(NodeTestTask.java:107)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:107)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:75)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:99)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
    at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at com.sun.proxy.$Proxy2.stop(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:133)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.ScriptEvaluationException
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 50 more


UnknownClass > initializationError FAILED
    java.lang.NoClassDefFoundError at Class.java:-2
        Caused by: java.lang.ClassNotFoundException at BuiltinClassLoader.java:581
1 test completed, 1 failed
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.

我的毕业档案是:

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
    id "org.openapi.generator" version "5.0.0-beta2"
    id "io.freefair.lombok" version "5.2.1" 
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'//crudrepo
    implementation 'org.springframework.security:spring-security-crypto'    
    runtimeOnly 'com.h2database:h2'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'org.springframework.security:spring-security-test'

    /* required per tutorial */
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
    testCompile 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
    testCompile 'org.mockito:mockito-junit-jupiter:2.23.0'

    /* added from other questions */
    testCompile 'org.junit.platform:junit-platform-launcher:1.4.0'
    testCompile 'org.junit.jupiter:junit-jupiter:5.4.0'
    testCompile group: 'org.junit.platform', name: 'junit-platform-commons', version: '1.7.1'

}

test {
    useJUnitPlatform()
}

...

所以看起来< code > org/JUnit/Jupiter/API/extension/ScriptEvaluationException 丢失了,因为我忘记了一个依赖项。

然而,该类应该是https://junit.org/junit5/docs/5.4.0/api/org/junit/jupiter/api/extension/ScriptEvaluationException.html因此包含在<code>testCompile‘org.junit.jupiter:junit-jupiter:5.4.0’

为什么会出现这个错误?我怎样才能解决这个问题?

共有1个答案

上官凯泽
2023-03-14

使用<code>testCompile‘org.junit.jupiter:junit-jupiter:5.4.0’

 类似资料:
  • 有没有办法集成测试我的JUnit Jupiter扩展?当然,我可以测试扩展用法的一个很好的例子,但我想测试如下内容: 它没有通过预期的测试吗? 测试结束时写的报告是否正确? 对此有内置的支持吗?

  • 或者只有一个依赖项就足够了? 我需要添加对的依赖项吗?

  • 问题内容: Maven模块和之间有什么区别?是否需要在其中包含两个依赖项? 我需要写两个依赖吗 要么 足够? 我是否需要增加对的依赖? 问题答案: 5.4之前的JUnit 从文档: JUnit Jupiter API,用于编写测试和扩展。 JUnit Jupiter测试引擎实现,仅在运行时需要。 JUnit Vintage测试引擎实现,允许在新的JUnit Platform上运行Vintage J

  • 如果使用重新运行jupiter扩展失败,我将尝试重复选定的测试。它在IntelliJ IDEA中运行良好,但在Jenkins上出现此消息时失败, 根本不执行任何测试,整个测试套件一开始就失败了。

  • 当我运行 :(32/0,省略)1100 0001 0111 1110 0000 0000 0000 < li> :(第32页,共1页,略)1100 0001 0111 1110 0000 0000 0000 为什么Javascript计算

  • 问题内容: 来自我的Web应用程序的XML响应既有HTML添加到页面,又有一些要运行的脚本。 我正在尝试从Web应用发送回XML,例如: 我现在正在做的是抢购和CDATA,将html插入页面并评估。 我正在寻找对我的方法的批评。任何人有什么建议吗? 问题答案: 您宁愿发送JSON,它也更易于解释。例: