首页 > 解决方案 > Spring Boot @SpringBootTest 给出 NoClassDefFoundError: ...DataAccessException

问题描述

我无法解决这个问题。我有一个spring boot应用程序。

带有注释的主类

@SpringBootApplication.

带有注释的测试类:

@RunWith(SpringRunner.class)
@SpringBootTest

一切都很好,直到我filter-configurer在应用程序中添加一个类:

@Configuration
public class YadaYadaFilterConfigurer
{
    @Bean
    public FilterRegistrationBean<YadaYadaFilter> createYadaYadaFilter() 
    {...}

    ...
}

繁荣!

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]: 
...
nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:...org.springframework.boot/spring-boot-test-autoconfigure/2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class]; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException

我的依赖:

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

一些澄清。剥离,以便我只有这个测试类:

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

    @Test
    public void contextLoads() {
    }
}

如果注释掉 @SpringBootTest 那么一切正常。似乎这个注释引入了对更多依赖项的需求?

更多堆栈跟踪:

原因:org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.boot.web.servlet.FilterRegistrationBean]:工厂方法'createyadayadaFilter'抛出异常;嵌套异常是 org.springframework.beans.factory.BeanDefinitionStoreException:无法读取候选组件类:URL [jar:file:/C:/Users/66122872/.gradle/caches/modules-2/files-2.1/org.springframework .boot/spring-boot-test-autoconfigure/2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest。班级]; 嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java: 185) 在 org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583) ... 67 更多原因:org.springframework.beans.factory.BeanDefinitionStoreException:无法读取候选组件类:URL [ jar:file:/C:/Users/66122872/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-test-autoconfigure/2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot -test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class]; 嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException BeanDefinitionStoreException:无法读取候选组件类:URL [jar:file:/C:/Users/66122872/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-test-autoconfigure/ 2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class];嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException BeanDefinitionStoreException:无法读取候选组件类:URL [jar:file:/C:/Users/66122872/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-test-autoconfigure/ 2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class];嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException

标签: javaspringspring-boot

解决方案


抱歉,经过更多调查后,我发现问题出在我的 FilterRegistrationBean 中。

在这里抛出异常。

ClassPathScanningCandidateComponentProvider provider = createComponentScanner();
provider.findCandidateComponents("").stream()...

我将回答这个线程并创建一个新线程。


推荐阅读