Having trouble using EnabledIfSystemProperty #5045
-
Currently using junit-jupiter v5.10.3. I need to be able to run or not run certain JUnit test classes or tests depending on the "environment". We will have a system property with a value of "dev", "test", "stage", or "prod". I would think that the following annotation could be put on a test class or method:
This would result in this class or method only being enabled if that system property has that value. I ran the following command line:
The IntegrationTestSuite class, which is a Suite, ran several test classes, one of which has that annotation, and it skipped the entire class. Any idea why that might be? I also have to figure out a scheme where I can define several wrapper annotation classes, like "EnableOnDevEnv" and "EnableOnTestEnv", and allow for a test class or method to have BOTH of these annotations. I'm not sure how I can get that to work (assuming I solve the first problem). |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Presumably the system property hasn't been set. Have you checked that the JVM started by Failsafe actually has that system property? |
Beta Was this translation helpful? Give feedback.
-
The docs say that the user property is It might be better to model that system property in your POM, though, because overriding The following allows to pass it via <project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.5.4</version>
<configuration>
<systemPropertyVariables>
<INTEGRATION_TEST_ENV>${integrationTestEnv}</INTEGRATION_TEST_ENV>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project> |
Beta Was this translation helpful? Give feedback.
This approach worked. However, it's curious to note that this is not the only "-D" property we set on this command line, and that other property is read in the test code itself, not by JUnit or a Maven plugin, and it requires no additional configuration to be able to access that property from the Java code. It's curious that accessing the property from "the infrastructure" requires this additional step.