【发布时间】:2019-04-16 12:09:27
【问题描述】:
我无法解决这个问题。我有一个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 在 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。班级];嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
【问题讨论】:
-
您还应该包含 spring-boot-starter-parent 依赖项。
-
如果这些是您唯一的依赖项,那么至少从异常来看,您缺少一些东西。显然,您在某处使用数据访问或在测试类中意外导入
@JdbcTest注释。但我怀疑您只添加了堆栈跟踪的 sn-p 并且原始堆栈跟踪是 langer。 -
@GauravRai1512 添加 spring-boot-starter-parent,仍然会产生同样的问题。
-
@M.Deinum 如果我取消注释“@SpringBootTest”一切正常。我剥离了所有东西,所以我只有一个什么都不做的空测试类。我在应用程序中没有数据访问权限。似乎“@SpringBootTest”包括数据访问依赖项。我会稍微更新一下我的帖子。
-
可能是对它有硬依赖的测试自动配置。您可能想要创建一个小测试用例并在 Spring Boot 问题跟踪器中创建一个问题。
标签: java spring spring-boot