从 Spring-启动测试中排除 elasticsearchTemplate
Exclude elasticsearchTemplate from Spring-Boot Test
我有一个使用 Elasticsearch 的应用程序,我想在测试某些控制器时禁用此集成。如何在 Spring-启动测试中禁用 elasticsearchTemplate?
Application.class:
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.closeupinternational.comclosure.elasticsearch")
public class Application {
...
Repository.class:
@Repository
public interface PipelineRepository extends ElasticsearchRepository<Pipeline, String> {
...
测试Controller.class:
@ExtendWith(SpringExtension.class)
@EnableAutoConfiguration(exclude = {ElasticsearchDataAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class})
@WebMvcTest(ProductionCycleExecutionController.class)
@Slf4j
public class ProductionCycleExecutionControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private ProductionCycleExecutionService prodCycleExecService;
...
我没有在 ProductionCycleExecutionService 内部使用,我现在不想尝试测试 elasticsearch 存储库 PipelineRepository。
错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pipelineRepository' defined in
com.closeupinternational.comclosure.elasticsearch.PipelineRepository defined in
@EnableElasticsearchRepositories declared on Application: Cannot resolve reference to bean
'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory
只需删除 @ExtendWith(SpringExtension.class)
和 @EnableAutoConfiguration(exclude = {ElasticsearchDataAutoConfiguration.class, ElasticsearchRepositoriesAutoConfiguration.class})
这些注释旨在 bootstrap 整个 Spring 上下文并对其进行配置
@WebMvcTest
在您的情况下应该足够了,因为它 bootstrap 仅与网络相关的上下文
更新
如果您的 ProductionCycleExecutionController
中有任何依赖项(如您提到的 elasticsearchTemplate
),那么如果您不需要定义它们的行为,则像这样模拟它们,如下所示:
@MockBeans(value = {@MockBean(YourBean1.class), @MockBean(YourBean2.class), @MockBean(YourBean3.class)})
如果您确实需要定义模拟行为,那么在 class:
中模拟为 属性
@MockBean
private YourBean yourBean;
我有一个使用 Elasticsearch 的应用程序,我想在测试某些控制器时禁用此集成。如何在 Spring-启动测试中禁用 elasticsearchTemplate?
Application.class:
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.closeupinternational.comclosure.elasticsearch")
public class Application {
...
Repository.class:
@Repository
public interface PipelineRepository extends ElasticsearchRepository<Pipeline, String> {
...
测试Controller.class:
@ExtendWith(SpringExtension.class)
@EnableAutoConfiguration(exclude = {ElasticsearchDataAutoConfiguration.class,
ElasticsearchRepositoriesAutoConfiguration.class})
@WebMvcTest(ProductionCycleExecutionController.class)
@Slf4j
public class ProductionCycleExecutionControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private ProductionCycleExecutionService prodCycleExecService;
...
我没有在 ProductionCycleExecutionService 内部使用,我现在不想尝试测试 elasticsearch 存储库 PipelineRepository。
错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pipelineRepository' defined in
com.closeupinternational.comclosure.elasticsearch.PipelineRepository defined in
@EnableElasticsearchRepositories declared on Application: Cannot resolve reference to bean
'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory
只需删除 @ExtendWith(SpringExtension.class)
和 @EnableAutoConfiguration(exclude = {ElasticsearchDataAutoConfiguration.class, ElasticsearchRepositoriesAutoConfiguration.class})
这些注释旨在 bootstrap 整个 Spring 上下文并对其进行配置
@WebMvcTest
在您的情况下应该足够了,因为它 bootstrap 仅与网络相关的上下文
更新
如果您的 ProductionCycleExecutionController
中有任何依赖项(如您提到的 elasticsearchTemplate
),那么如果您不需要定义它们的行为,则像这样模拟它们,如下所示:
@MockBeans(value = {@MockBean(YourBean1.class), @MockBean(YourBean2.class), @MockBean(YourBean3.class)})
如果您确实需要定义模拟行为,那么在 class:
中模拟为 属性@MockBean
private YourBean yourBean;