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

前端 未结 5 949
梦谈多话
梦谈多话 2020-12-10 13:47

I have a spring-boot application for which I am writing IT tests.

The data for the tests comes from application-dev.properties when I activ

相关标签:
5条回答
  • 2020-12-10 14:10

    You can get the active profiles using org.springframework.core.env.Environment: (Beware, it's Kotlin.)

    @Autowired
    private val environment: Environment? = null
    
    private fun isProfileActive(name: String) = environment!!.activeProfiles.contains(name)
    
    @Test fun onlyOnOpenShift {
         org.junit.Assume.assumeTrue(isProfileActive("openshift"));
         ...
    }
    

    If you have a lot of cases where you would use it (which I would suggest might hint that you're doing something wrongly), then it may pay off to decorate the test method with an annotation like @OnlyIfProfileActive("openshift") and process it with your own JUnit extension, like, implementing org.junit.jupiter.api.extension.BeforeTestExecutionCallback and determine if the method should run. In such case, the environment can be obtained from the Spring test runner.

    @Test @OnlyIfProfileActive("openshift")
    fun onlyOnOpenShift {
         ...
    }
    

    Which, I suspect, is what annotations like @IfProfileValue do. But here you would have more control.

    For instance, to make it versatile, it could be an expression evaluated with JEXL or JUEL or such.

    @Test @SpringProfilesCondition("openshift && !mockDatabase")
    fun onlyOnOpenShift {
         ...
    }
    
    0 讨论(0)
  • 2020-12-10 14:16

    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

    0 讨论(0)
  • 2020-12-10 14:20

    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.

    0 讨论(0)
  • In Spring you can also use the @DisabledIf annotation. It allows for specifying a Spring Expression Language expression. See this blog post for examples.

    JUnit 5 also has:

    • @DisabledIfEnvironmentVariable
    • @DisabledIfSystemProperty
    0 讨论(0)
  • 2020-12-10 14:30

    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.

    0 讨论(0)
提交回复
热议问题