Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/Assume$AssumptionViolatedException

Exception in thread "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.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.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.run(URLClassLoader.java:366)
    at java.net.URLClassLoader.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

我还应该做些什么来让它发挥作用吗?或者这只是一个错误?

你实际上没有使用spring-test 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 的作者)