我正在为使用Couchbase信息库的反应式Web应用程序编写一些单元测试,但是由于试图ApplicationContext
时抛出NoSuchBeanDefinitionException
,@Autowire
无法加载{1}}。
在src / main / java中,应用程序运行良好,我可以从PostMan发送请求并获得有效的响应。不需要配置类。我的application.properties设置如下:
ReactiveCouchbaseRepository
我创建了一个src / test / resources文件夹,将其标记为测试资源文件夹(在IntelliJ中),并复制到application.properties上,但是仍然抛出相同的异常。我还在测试文件夹中创建了一个ouchbase配置类,如下所示:
spring.couchbase.bootstrap-hosts=subdomain.domain.com
spring.couchbase.bucket.name=some_bucket_name
spring.couchbase.bucket.password=a_password
spring.couchbase.env.timeouts.connect=10000
我的测试类如下:
@Configuration
@EnableCouchbaseRepositories
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
@Value("${spring.couchbase.bootstrap-hosts}")
private String hosts;
@Value("${spring.couchbase.bucket.name}")
private String bucketName;
@Value("${spring.couchbase.bucket.password}")
private String password;
@Override
protected List<String> getBootstrapHosts() {
return Collections.singletonList(hosts);
}
@Override
protected String getBucketName() {
return bucketName;
}
@Override
protected String getBucketPassword() {
return password;
}
}
抛出以下异常(为简便起见,省略了完整的异常堆栈,仅显示了第一个和最后一个异常):
@RunWith(SpringRunner.class)
@WebFluxTest
public class ReactiveSpringApplicationTests {
@Autowired
WebTestClient webTestClient;
@Test
public void create_Test_Post_Event() throws Exception {
EventData eventData = new EventData();
eventData.setIdentifier("1111");
webTestClient.post().uri("/ad/acc")
.contentType(MediaType.APPLICATION_JSON_UTF8) // Set the media type of the body, as specified by the Content-Type header.
.accept(MediaType.APPLICATION_JSON_UTF8) // Set the list of acceptable media types, as specified by the Accept header.
.header("Corr", "0000")
.body(Mono.just(eventData), EventData.class) // Set the body of the request to the given asynchronous Publisher.
.exchange() // Perform the exchange without a request body.
.expectStatus().isCreated()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBody(EventData.class);
}
}
({java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.amex.gar.accounts.repository.CouchBaseDocRepository<?, ?>' available: expected at least 1 bean which qualifies as autowire candidate.
扩展CouchBaseDocRepository
)
如何获取ApplicationContext以便从测试目录中正确识别Couchbase存储库?