Search code examples
javaspringjunitnoclassdeffounderrorspring-test

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


I've set up a Spring project, and I got an exception when running the following test.

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);
    }
}

The exception is as follows.

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

I found that AssumptionViolatedException was located in org.junit.AssumptionViolatedException instead of org.junit.Assume, I think maybe this is reason why the exception was thrown.

But if I remove the following annotation lines, it would work just fine

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

The output is:

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

Most answers I got from the Internet told me that I should use junit and spring-test with a higher version. But I think I'm using the highest versions for both of them.

junit: 4.12
spring-test: 4.3.2.RELEASE

Is there any other things I should do to make it work? Or is it just a bug?


Solution

  • You are actually not using spring-test 4.3.2.

    Rather, you have a very old version of spring-test on your classpath.

    SpringMethodRoadie was introduced in Spring 2.5 for JUnit 4.4 support, and if I recall correctly, SpringMethodRoadie was removed in Spring 3.0 with the upgrade to JUnit 4.5.

    The solution is to configure your build or IDE to use the correct version of spring-test.

    Regards,

    Sam (author of the Spring TestContext Framework)