我有几个@Component注解的类,我已经在其中初始化了我的配置器,比如DBConfigurer、SecurityConfigurer、JmxConfigurer等等。
在添加功能测试时,我希望有几个类不应该被加载。我可以排除上面定义的几个配置器吗?
我定义的测试类如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { ServiceStarter.class }, properties = { "jmx.rmi.port=19057", "hostname=localhost" })
@ContextConfiguration(classes = {ControllerTest.BeansOverrideConfigurer.class})
public class ControllerTest {
// All test here.
}我可以在测试类的@ContextConfiguration块中定义选定的配置器,但我不想这样做,因为将来如果有人添加了新的配置器,它应该会自动导入到测试用例中。
发布于 2020-02-22 22:23:26
在那些你不想在特定配置文件期间加载的组件上使用@Profile,例如测试(即仅在生产和开发期间)。
@Component
@Profile("production")
public class ComponentClass {}然后定义要在测试期间运行的@ActiveProfile。
@SpringBootTest
@ActiveProfiles("test")
public class ControllerTest {}你可以参考Spring Framework文档和Spring Boot文档获取更多信息docs.spring.io
https://stackoverflow.com/questions/60332681
复制相似问题