[英]Excluding Configuration in Spring boot test
I have the following setup in a maven project.我在 Maven 项目中有以下设置。 Configuration class for the productive code:
生产代码的配置类:
package com.example;
@Configuration
public class MyConfiguration {
@Bean
public A beanA() {
return new A();
}
...
}
Then I have one test, that has an internal Configuration
:然后我有一个测试,它有一个内部
Configuration
:
package com.example;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {MyConfiguration.class, SpringConfigurationTest.MyTestConfiguration.class})
public class SpringConfigurationTest {
@TestConfiguration
static class MyTestConfiguration {
@Bean
@Primary
public A beanA() {
return mock(A.class);
}
}
}
The tests in this class work fine.本课程中的测试工作正常。 Then I do have another Test class in sub package:
然后我在子包中有另一个 Test 类:
package com.example.sub;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {MyConfiguration.class, AnotherSpringConfigurationTest.MyTestConfiguration.class})
public class AnotherSpringConfigurationTest {
@TestConfiguration
static class MyTestConfiguration {
@Bean
public B beanB() {
return new B()
}
}
}
When running tests in this class the test configuration from SpringConfigurationTest.MyTestConfiguration
is also included.在此类中运行测试时,还包括来自
SpringConfigurationTest.MyTestConfiguration
的测试配置。 My guess the reason for this is the inclusion of MyConfiguration
which lies in the root directory.我的猜测原因是包含位于根目录中的
MyConfiguration
。 The effect is that in AnotherSpringConfigurationTest
the bean A
is mocked instead of a real instance.效果是在
AnotherSpringConfigurationTest
,bean A
被AnotherSpringConfigurationTest
而不是真实实例。
How can I avoid that configuration classes inside other tests are 'leaked' into other tests?如何避免其他测试中的配置类“泄露”到其他测试中?
I have seen Spring-boot default profile for integration tests which uses Spring profiles in Spring Boot 1.4.1, while I am using Spring Boot 2.0.1.我已经看到Spring-boot 默认配置文件用于集成测试,它在 Spring Boot 1.4.1 中使用 Spring 配置文件,而我使用的是 Spring Boot 2.0.1。 Though I am sure it could be done.
虽然我确信它可以做到。
Another approach I could think of would be to use component scanning to pick up the contexts and exclude all those that I do not want, but that is a bit cumbersome, as I would much prefer an approach where I define what I want to use instead of what should not be used.我能想到的另一种方法是使用组件扫描来获取上下文并排除所有我不想要的上下文,但这有点麻烦,因为我更喜欢一种方法来定义我想要使用的内容不应该使用的东西。
Is there an easy and elegant solution with Spring Boot 2 to avoid conflicting context configurations? Spring Boot 2 是否有一个简单而优雅的解决方案来避免上下文配置冲突?
First, I don't like the idea of putting a config class in another class.首先,我不喜欢将配置类放在另一个类中的想法。 I don't know if that is frenquently made, I'm kinda new to Spring.
我不知道这是否经常制作,我对 Spring 有点陌生。 But here is what I would do :
但这是我会做的:
First config file :第一个配置文件:
@Configuration
@Import(MyConfiguration.class)
public class MyTestConfiguration {
@Bean
@Primary // added to let know on @Autowired to use this one instead of the one in first MyConfiguration.class
public A beanA() {
return mock(A.class);
}
}
Second config file :第二个配置文件:
@Configuration
@Import(MyConfiguration.class)
public class MyOtherTestConfiguration {
@Bean
public B beanB() {
return new B()
}
}
I'm not familiar with @TestConfiguration
.我不熟悉
@TestConfiguration
。 That's why i'm using @Configuration
.这就是我使用
@Configuration
的原因。
First test class :第一个测试类:
package com.example;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MyTestConfiguration.class)
public class SpringConfigurationTest {
}
Second test class :第二个测试类:
package com.example.sub;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MyOtherTestConfiguration.class)
public class AnotherSpringConfigurationTest {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.