我正在使用帶有 webflux 的 spring-boot (2.2.7.RELEASE) 來提供帶有 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 名稱:DefaultTheService Test
通常,我可以在每次測驗之前簡單地創建一個 DefaultTheService 的實體,然后呼叫我想要測驗的方法,但我想嘗試使用 spring。
如果我只是洗掉private DefaultTheService defaultTheService宣告 - 測驗正在“運行”。我很確定我錯過了一些愚蠢的東西,我正在追逐我的尾巴。
那么,有人可以減輕我的痛苦并指出我正在犯的(可能?)明顯錯誤嗎?
謝謝!
uj5u.com熱心網友回復:
@DataMongoTest:
可用于僅關注 MongoDB 組件的 MongoDB 測驗的注釋。
使用此注釋將禁用完全自動配置,而是僅應用與 MongoDB 測驗相關的配置。
嘗試使用@SpringBootTest“完整”/默認應用程式背景關系。
有關一般資訊,請參閱:
- https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#testing
對于(自動)配置細節和改進:
- https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing
... 設定@DataMongoTest(useDefaultFilters = false)( 微調include-/excludeFilters)也可以做想要的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/384747.html
上一篇:If陳述句內映射
下一篇:MongoDB按多個欄位分組
