Issue
My goal is to execute tests in test class only when some spring property declared in application-foo.properties
is equal to some constant.
I've tried to reach this by writing my own ExecutionCondition with annotation using it, and when I use it on @Test
method it works fine. But using one on a test class leads to the problem: condition evaluates before Spring's context is up.
According to the docs:
If an ExecutionCondition disables a test method, that does not prevent the test class from being instantiated.
But what should I do if I want to?
Script-based condition APIs and their supporting implementations are deprecated with the intent to remove them in JUnit Jupiter 5.6. Users should instead rely on a combination of other built-in conditions or create and use a custom implementation of ExecutionCondition to evaluate the same conditions.
Since v5.6.0 the @EnabledIf annotation was excluded. One of advantages of this one is loadContext()
property, which allows to reach the goal of this topic, literally.
None of remaining annotations (e.g. EnabledIfEnvironmentVariable) contain even a tiny hint, how to reach it. And I haven't found any possibilities in the User Guide as well.
I'll appreciate any help or advice.
Solution
Indeed @EnabledIf
from import org.junit.jupiter.api.condition.EnabledIf
was removed in JUnit 5.6.
There is also @EnabledIf
from Spring Test import org.springframework.test.context.junit.jupiter.EnabledIf
which is not deprecated and fits your use case.
@EnabledIf(
expression = "#{systemProperties['your.property'].toLowerCase().contains('disabled')}",
reason = "Disabled due to property",
loadContext = true
)
@SpringBootTest
public class MyTest {
// your tests
}
Let's say you have a property inside application-foo.properties
:
flaky.tests.enabled = false
You can now conditionally run the tests with
@EnabledIf(
expression = "${flaky.tests.enabled}",
reason = "Disabled flaky tests",
loadContext = true
)
@SpringBootTest
@ActiveProfiles("foo")
public class FlakyTest {
// your tests
}
I tested it with Spring Boot 2.3.0 and it works fine without any deprecation warning.
Answered By - rieckpil