NoClassDefFoundError ErrorCoded

Viewed 25199

I am trying to run a JUnit test in my Spring Boot app, but I am getting the NoClassDefFoundError shown below.

Both the spring-boot-starter-parent and spring-boot-starter-test (v2.0.5.RELEASE) are in the pom.xml file. I added the spring-core (v5.0.9.RELEASE) in as well.

Apparently, the ErrorCoded class has been deprecated as of 4.3.6, so I'm not sure how to find out why the test runner (or other library) is still trying to load this class.

Here is my test class:

@RunWith(SpringJUnit4ClassRunner.class)
public class SystemBuilderTest {
    
    private System system;

    @Before
    public void setUp() throws Exception {
        StatefulConnection conn = new StatefulConnection.Builder(null)
                .build();
        
        Device d1 = new SensingDevice.Builder("sensor1", conn)
                .build();
        
        system = new System.SystemBuilder("testSystem")
                .addChildDevice(d1)
                .build();
        
        system.initialize();
    }

    @Test
    public void testStart() throws DCFDeviceException {
        system.start();
        assertTrue(system.getName().equals("testSystem"));
        assertTrue(system.getChildDevices().size() == 1);
        assertTrue(system.getChildDevices().iterator().next().getName().equals("sensor1"));
        
    }
} 

... and the stack trace:

java.lang.NoClassDefFoundError: org/springframework/core/ErrorCoded
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:801)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:699)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:622)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:580)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:801)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:699)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:622)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:580)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:110)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:115)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:275)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.ClassNotFoundException: org.springframework.core.ErrorCoded
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    ... 50 common frames omitted
4 Answers

Something in your build is pulling in a dependency on spring-test 4.3.x (not sure what minor release, but it shouldn't be relevant.)

The reason I say this is because the line numbers in the stack trace do not align with the line numbers in the SpringJUnit4ClassRunner class in the 5.x releases.

From your stack trace:

at o.s.t.c.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)

That indicates that at line 227 there should be a call to a createTest method. You can see that this is true in the 4.3.x branch version of this class.

But in the 5.0.x branch version, line 227 is instead a call to getTestContextManager().prepareTestInstance(testInstance);.

This is pretty conclusive. So if your IDE is telling you otherwise (as you indicated in another comment), it is wrong. Or you might need to do a 'clean' on your project, if the IDE supports that functionality.

Since you did not post your full pom it is not possible to tell you what is pulling in the old dependency. However, you should be able to figure it out by using the Maven dependency plugin:

mvn dependency:tree

This command will print the dependency hierarchy in tree form. I recommend redirecting output to file and grepping for spring-test. That will allow you to identify the problematic dependency and use a maven <exclusion> to disallow the problematic dependency.

I cannot answer this exactly why is it error but I can tell that you have the conflict in version of some dependency.

As you can see in the document below(although you use version 2.0.5.RELEASE).

https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/

ErrorCoded is used by SpringJUnit4ClassRunner but if you see in their Github, SpringJUnit4ClassRunner does not use ErrorCoded anymore in version 5.0.8 of org.springframework.spring-test.

You have to see if your Spring project really uses which version of org.springframework.spring-test because SpringJUnit4ClassRunner is in that jar.

Hope this helps.

Can you pleaser un your test with SpringRunner.class and @SpringBootTest will load all required test dependencies.

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

For me happened to be older version of spring-test pulled by the spring-security-test. I am using spring-boot 2.x.

λ mvn dependency:tree | grep test
[INFO] +- org.springframework.security:spring-security-test:jar:4.2.3.RELEASE:test
[INFO] |  \- org.springframework:spring-test:jar:4.3.9.RELEASE:test

Updating spring-security and spring-security-test to 5.x fixed it.

<properties>
    <java.version>1.8</java.version>
    <spring.version>2.0.4.RELEASE</spring.version>
    <logging.version>2.8.2</logging.version>
    <spring-security-test.version>5.0.6.RELEASE</spring-security-test.version>
    <spring-security-config.version>5.0.6.RELEASE</spring-security-config.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring-security-config.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring-security-config.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <version>${spring-security-test.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>
Related