是否可以标记springboot测试,以便它们仅在某些配置文件处于活动状态时运行

IKo*_*IKo 11 java testing spring-boot

我有两个配置文件:dev和default.当活动配置文件是默认时,我想跳过一些(不是全部)测试.有可能以某种方式标记这些测试吗?或者如何实现这一目标?我用springboot.这是我的父测试类:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyServiceStarter.class, webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT,
        properties = {"flyway.locations=filesystem:../database/h2", "server.port=9100", "spring.profiles.default=dev"})
@Category(IntegrationTest.class)
public abstract class AbstractModuleIntegrationTest { ... }
Run Code Online (Sandbox Code Playgroud)

IKo*_*IKo 10

我的同事找到了一个解决方案:所以如果你需要注释单独的测试,你可以使用@IfProfileValue注释:

@IfProfileValue(name ="spring.profiles.active", value ="default")
    @Test
    public void testSomething() {
        //testing logic
    }
Run Code Online (Sandbox Code Playgroud)

此测试仅在默认配置文件处于活动状态时运行


Pat*_*ick 5

是的,你可以做到。

例如使用@ActiveProfiles

@ActiveProfiles("default")
@RunWith(SpringRunner.class)
@SpringBootTest
public class YourTest {
   //tests
}
Run Code Online (Sandbox Code Playgroud)

  • 这是错误的。“@IfProfileValue”不决定测试是否运行。它仅决定测试运行时应使用哪些 bean 定义配置文件。“@IfProfileValue”应按照接受的答案使用。有关详细说明,请参阅/sf/ask/1652524261/ (2认同)