how to run/turn off selective tests based on profiles in spring boot

蓝咒 提交于 2019-12-05 03:39:58

You would want to use the @IfProfileValue annotation. Unfortunately it doesn't work directly on the active profiles but it can read a property so if you only define a specific property within the profiles that you want to run the test on then you can use that annotation on that specific property.

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#integration-testing-annotations-junit

Nathan Niesen

I wanted to exclude tests that required an external service but I couldn't get that to work the way I wanted (more or less a non-existent @IfNotProfileValue).

As an alternative, I used the JUnit Assume.assumeThat which provided the behavior I wanted. e.g.,

Assume.assumeThat("Skipping Test: No username property", this.username, not(isEmptyString()));

I ended up not using a profile to drive it but you should be able to define a property or use the Environment to determine if a profile is active.

The assumeThat can be used in @Test and @Before methods but be aware that the @After methods will still run so cleanup might need a code guard.

It works also with active profiles - there is a property value containing active profiles:

Test only active with specific profile:

@IfProfileValue(name = "spring.profiles.active", values = {"specific"})

Since i have tests that should NOT run if specific profile is active i added this to those tests:

@ActiveProfiles(profiles = {"default"})

It does not work with @IfProfileValue and "default" and i also didn't found any "run if specific profile is not active.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!