【问题标题】:With TestNG+Spring, how to make @Bean configuration local to just one test?使用 TestNG+Spring,如何将 @Bean 配置本地化为一个测试?
【发布时间】:2016-11-25 16:00:01
【问题描述】:

我在我的一个 TestNG 测试中添加了 @Configuration 注释和 @Bean 定义,以便用模拟覆盖深层 @Autowired 对象。

问题是,这弄乱了我所有的其他集成测试,我需要真正的@Autowired 对象。如何将 @Bean 配置本地化到我想要模拟的一个测试?

我已经尝试过 @DirtiesContext(classMode=ClassMode.AFTER_CLASS) 注释,但这次没有用(尽管我在其他情况下也成功使用过)。

【问题讨论】:

    标签: spring unit-testing testing testng integration-testing


    【解决方案1】:

    添加这两个类级别的注释:

    @ActiveProfiles({"this-test-name"})
    @Profile("this-test-name")
    

    其中this-test-name 是导致问题的特定测试的唯一名称。

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2016-04-25
      • 2019-11-12
      相关资源
      最近更新 更多