在集成测试期间使用嵌入式 mongo 连接 bean
Wiring beans during integration tests with embedded mongo
我正在将 spring-boot (2.2.7.RELEASE) 与 webflux 一起用于 mongodb 的小型休息服务。我有 2 个存储库(ARepository
、BRepository
)实现了如下内容:
@Repository
public interface ARepository extends ReactiveMongoRepository<DataDTO, Integer> {
}
我还有一个额外的服务,它使用这两个和一个 ReactiveMongoTemplate 实例。它是这样连接的:
@Slf4j
@Service
public class DefaultTheService implements TheService {
private final ARepository aRepository;
private final BRepository bRepository;
private final ReactiveMongoTemplate mongoTemplate;
@Autowired
public DefaultTheService(ARepository aRepository, BRepository bRepository, ReactiveMongoTemplate mongoTemplate) {
this.aRepository = aRepository;
this.bRepository = bRepository;
this.mongoTemplate = mongoTemplate;
}
}
一切都很好,一切正常,没有问题。
现在,我想写一些集成测试,我是这样开始的:
@DataMongoTest
@Slf4j
class DefaultTheServiceTest {
@Autowired
private ARepository aRepository;
@Autowired
private BRepository bRepository;
@Autowired
private ReactiveMongoTemplate reactiveMongoTemplate;
@Autowired
private DefaultTheService defaultTheService;
@Test
void runTheMagicTest() {
// empty body, I just want to see if everything wires up correctly.
}
}
当我想执行runTheMagicTest
(junit5)时,我总是得到这个错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.DefaultTheServiceTest': Unsatisfied dependency expressed through field 'defaultTheService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.DefaultTheService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.DefaultTheService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1716)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1272)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1226)
*!注意 bean 名称:DefaultTheServiceTest
通常,我可以在每次测试之前简单地创建一个 DefaultTheService 实例,然后调用我想要测试的方法,但我想尝试使用 spring。
如果我简单地删除 private DefaultTheService defaultTheService
声明 - 测试是“运行”。我很确定我错过了一些愚蠢的东西,我正在追尾。
那么,有人可以减轻我的痛苦并指出我正在犯的(可能吗?)明显的错误?
谢谢!
Annotation that can be used for a MongoDB test that focuses only on MongoDB components.
Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MongoDB tests.
改为尝试 @SpringBootTest
以获得“完整”/默认应用程序上下文。
有关一般信息,请参阅:
对于(自动)配置详细信息和改进:
...设置@DataMongoTest(useDefaultFilters = false)
(+微调include-/excludeFilters
)也可以达到预期效果。
我正在将 spring-boot (2.2.7.RELEASE) 与 webflux 一起用于 mongodb 的小型休息服务。我有 2 个存储库(ARepository
、BRepository
)实现了如下内容:
@Repository
public interface ARepository extends ReactiveMongoRepository<DataDTO, Integer> {
}
我还有一个额外的服务,它使用这两个和一个 ReactiveMongoTemplate 实例。它是这样连接的:
@Slf4j
@Service
public class DefaultTheService implements TheService {
private final ARepository aRepository;
private final BRepository bRepository;
private final ReactiveMongoTemplate mongoTemplate;
@Autowired
public DefaultTheService(ARepository aRepository, BRepository bRepository, ReactiveMongoTemplate mongoTemplate) {
this.aRepository = aRepository;
this.bRepository = bRepository;
this.mongoTemplate = mongoTemplate;
}
}
一切都很好,一切正常,没有问题。
现在,我想写一些集成测试,我是这样开始的:
@DataMongoTest
@Slf4j
class DefaultTheServiceTest {
@Autowired
private ARepository aRepository;
@Autowired
private BRepository bRepository;
@Autowired
private ReactiveMongoTemplate reactiveMongoTemplate;
@Autowired
private DefaultTheService defaultTheService;
@Test
void runTheMagicTest() {
// empty body, I just want to see if everything wires up correctly.
}
}
当我想执行runTheMagicTest
(junit5)时,我总是得到这个错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.DefaultTheServiceTest': Unsatisfied dependency expressed through field 'defaultTheService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.DefaultTheService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.DefaultTheService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1716)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1272)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1226)
*!注意 bean 名称:DefaultTheServiceTest
通常,我可以在每次测试之前简单地创建一个 DefaultTheService 实例,然后调用我想要测试的方法,但我想尝试使用 spring。
如果我简单地删除 private DefaultTheService defaultTheService
声明 - 测试是“运行”。我很确定我错过了一些愚蠢的东西,我正在追尾。
那么,有人可以减轻我的痛苦并指出我正在犯的(可能吗?)明显的错误?
谢谢!
Annotation that can be used for a MongoDB test that focuses only on MongoDB components.
Using this annotation will disable full auto-configuration and instead apply only configuration relevant to MongoDB tests.
改为尝试 @SpringBootTest
以获得“完整”/默认应用程序上下文。
有关一般信息,请参阅:
对于(自动)配置详细信息和改进:
...设置@DataMongoTest(useDefaultFilters = false)
(+微调include-/excludeFilters
)也可以达到预期效果。