首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在SpringBootTest中排除一些@Component注解的类

在SpringBootTest中排除一些@Component注解的类
EN

Stack Overflow用户
提问于 2020-02-21 13:28:57
回答 1查看 93关注 0票数 1

我有几个@Component注解的类,我已经在其中初始化了我的配置器,比如DBConfigurer、SecurityConfigurer、JmxConfigurer等等。

在添加功能测试时,我希望有几个类不应该被加载。我可以排除上面定义的几个配置器吗?

我定义的测试类如下所示:

代码语言:javascript
运行
复制
@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块中定义选定的配置器,但我不想这样做,因为将来如果有人添加了新的配置器,它应该会自动导入到测试用例中。

EN

回答 1

Stack Overflow用户

发布于 2020-02-22 22:23:26

在那些你不想在特定配置文件期间加载的组件上使用@Profile,例如测试(即仅在生产和开发期间)。

代码语言:javascript
运行
复制
@Component
@Profile("production")
public class ComponentClass {}

然后定义要在测试期间运行的@ActiveProfile。

代码语言:javascript
运行
复制
@SpringBootTest
@ActiveProfiles("test")
public class ControllerTest {}

你可以参考Spring Framework文档和Spring Boot文档获取更多信息docs.spring.io

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60332681

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档