线程“main”中的异常java.lang.NoClassDefFoundError:org / junit / Assume $AssumptionViolatedException

我已经设置了一个Spring项目,运行以下测试时遇到异常.

package com.company.app;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-config.xml"})
public class CheckTest {

    @Test
    public void test_foo() {
        assertEquals(0, 1);
    }
}

例外情况如下.

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/Assume$AssumptionViolatedException
    at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:281)
    at org.springframework.test.context.junit4.SpringMethodRoadie$2.run(SpringMethodRoadie.java:207)
    at org.springframework.test.context.junit4.SpringMethodRoadie.runBeforesThenTestThenAfters(SpringMethodRoadie.java:254)
    at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:234)
    at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:204)
    at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:146)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:151)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:52)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:50)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 22 more

我发现AssumptionViolatedException位于org.junit.AssumptionViolatedException而不是org.junit.Assume,我想也许这就是抛出异常的原因.

但是,如果我删除以下注释行,它将工作得很好

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/spring-config.xml"})

输出是:

java.lang.AssertionError: 
Expected :0
Actual   :1

我从互联网上得到的大多数答案告诉我,我应该使用更高版本的junit和spring-test.但我认为我正在使用它们的最高版本.

junit: 4.12
spring-test: 4.3.2.RELEASE

我还有什么别的办法可以让它起作用吗?或者它只是一个错误?

解决方法:

你实际上没有使用弹簧测试4.3.2.

相反,你在类路径上有一个非常旧版本的spring-test.

SpringMethodRoadie是在Spring 2.5中为JUnit 4.4支持引入的,如果我没记错的话,SpringMethodRoadie在Spring 3.0中被删除,升级到JUnit 4.5.

解决方案是配置您的构建或IDE以使用正确版本的spring-test.

问候,

Sam(Spring TestContext Framework的作者)

上一篇:android – Urban Airship崩溃应用程序


下一篇:更新到Spring 5.1 – java.lang.NoClassDefFoundError:org / springframework / http / server / reactive /