jot*_*87r 5 java spring-data-mongodb spring-boot
我有一个 Spring Boot 应用程序,我在其中启用了 mongo 审核。应用程序启动正常,并存储带有所有审核字段(createdDate、updatedDate 等)的 mongo 文档。但是,在运行单元测试时,出现以下异常:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoAuditingHandler': Cannot create inner bean '(inner bean)#115c946b' of type [org.springframework.data.mongodb.config.MongoAuditingRegistrar$MongoMappingContextLookup] while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#115c946b': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#115c946b': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Run Code Online (Sandbox Code Playgroud)
这是我的主要课程
@SpringBootApplication
@EnableMongoAuditing
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(PatientstoreApplication.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试课:
@WebMvcTest(TestController.class)
public class TestTest {
@Autowired MockMvc mockMvc;
@MockBean TestService testService;
@Test
public void test() {
}
}
Run Code Online (Sandbox Code Playgroud)
可以在这里找到可重现的示例:https://github.com/jota87r/testapp
我的问题是:是否缺少任何配置?我应该为我的测试用例手动创建 MappingMongoConverter 类型的 bean 吗?
我遇到了同样的情况。
在您的@WebMvcTest班级中,尝试添加测试配置,例如:
@TestConfiguration
static class TestConfig {
@Bean
public MappingMongoConverter mongoConverter() {
return new MappingMongoConverter(mock(DbRefResolver.class), mock(MappingContext.class));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1758 次 |
| 最近记录: |