NoClassDefFoundError ErrorCoded

时间:2018-09-18 23:31:55

标签: java spring spring-boot junit

我正在尝试在我的SpringBoot应用程序中运行JUnit测试,但出现如下所示的NoClassDefFoundError。

spring-boot-starter-parent和spring-boot-starter-test(v2.0.5.RELEASE)都位于pom.xml文件中。我还添加了spring-core(v5.0.9.RELEASE)。

显然,从4.3.6版开始,ErrorCoded类已被弃用,因此我不确定如何找出测试运行器(或其他库)为何仍尝试加载此类的原因。

这是我的考试班:

@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"));

    }
} 

...以及堆栈跟踪:

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 个答案:

答案 0 :(得分:0)

我不能完全回答为什么会出错,但是我可以告诉您某些依赖版本存在冲突。

如下面的文档所示(尽管您使用的是2.0.5.RELEASE版本)。

  

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

SpringJUnit4ClassRunner使用了ErrorCoded,但是如果在他们的Github中看到它们,则org.springframework.spring-test的5.0.8版本中的SpringJUnit4ClassRunner不再使用ErrorCoded。

您必须查看您的Spring项目是否真的使用了哪个版本的org.springframework.spring-test,因为SpringJUnit4ClassRunner位于该jar中。

希望这会有所帮助。

答案 1 :(得分:0)

您可以用SpringRunner.class进行测试吗?@SpringBootTest将加载所有必需的测试依赖项。

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

答案 2 :(得分:0)

您的构建中有一些东西依赖于Spring-test 4.3.x(不确定哪个次要版本,但它不相关)。

之所以这样说,是因为堆栈跟踪中的行号与5.x版本中的SpringJUnit4ClassRunner类中的行号不一致。

从堆栈跟踪中:

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

这表示在第227行应该调用createTest方法。您可以在此类的4.3.x branch version中看到这是真的。

但是在5.0.x branch version中,第227行是对getTestContextManager().prepareTestInstance(testInstance);的调用。

这是非常确定的。因此,如果您的IDE告诉您其他方式(如您在另一条注释中所述),那是错误的。或者,如果IDE支持该功能,则可能需要对项目进行“清理”。

由于您没有发布完整的pom,因此无法告诉您是什么原因导致了旧的依赖项。但是,您应该可以通过使用Maven依赖插件来解决它:

mvn dependency:tree

此命令将以树形式打印依赖关系层次结构。我建议将输出重定向到文件并grepping以进行弹簧测试。这样您就可以识别出有问题的依赖关系,并使用maven <exclusion>来禁止有问题的依赖关系。

答案 3 :(得分:0)

对我来说,它是由spring-security-test拉出的弹簧测试的旧版本。我正在使用 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

将spring-security和spring-security-test更新为5.x进行了修复。

<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>