【问题标题】:Conditional JUnit 4 test execution based on Spring profiles基于 Spring 配置文件的条件 JUnit 4 测试执行
【发布时间】:2019-07-01 09:56:33
【问题描述】:

我有一个应用程序,它使用"spring.profiles.active" 来决定使用哪个配置文件。我们正在使用 JUnit 4。我需要根据使用的 Profile 来调整我的测试类。所以如果"spring.profiles.active" = "X"我需要在A类中执行测试,如果"spring.profiles.active" = "Y"我需要在B类中执行测试。

我尝试过使用

@IfProfileValue(name="spring.profiles.active", value="X")

但是带有上述注解的类永远不会被执行。

System.Properties()
System.getProperties(){java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=<<Masked>>, java.vm.version=25.191-b12, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=;, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, file.encoding.pkg=sun.io, user.country=US, user.script=, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=, java.vm.specification.name=Java Virtual Machine Specification, user.dir=<<Masked>>, java.runtime.version=1.8.0_191-b12, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, , os.arch=amd64, spring.profiles.active=X

【问题讨论】:

    标签: java spring junit spring-profiles


    【解决方案1】:

    希望您在班级级别添加了@IfProfileValue(name="spring.profiles.active", value="X").. 尝试在类中添加测试方法并检查。

    【讨论】:

    • 我在类级别和方法级别都试过了 //@ContextConfiguration(locations = { "classpath:>" }) //@RunWith(PowerMockRunner.class) // @PrepareForTest(>.class) @IfProfileValue(name="spring.profiles.active", value="X") 公共类 > ...
    猜你喜欢
    • 1970-01-01
    • 2019-02-20
    • 2020-10-19
    • 2023-03-23
    • 2012-09-16
    • 1970-01-01
    • 2018-03-07
    • 2017-12-25
    • 2010-10-02
    相关资源
    最近更新 更多