What is the recommended solution to run a test multiple times with different system properties via command line?
What I would like to do could look like this:
gradle clean test --tests -Dmyproperty=foo my.fancy.test.TestClass --tests -Dmyproperty=bar my.fancy.test.TestClass
I need this parameter for the setup code in the @BeforeAll
method.
CodePudding user response:
As you use junit you can use @ParameterizedTest
which allows you to repeat the same junit test with different parameters.
You would need to move the code for the setup from the "BeforeAll" somewhere else.
static Stream<String> testArgs() { return Stream.of(""," ", null,"12345"); }
@ParameterizedTest
@MethodSource("testArgs")
public void testByArgs(String arg){
// Your test with asserts based on args here ...
}
CodePudding user response:
You need to also wire those parameters up with tests' JVM through Gradle's test dsl:
// build.gradle
test {
systemProperty 'myproperty', System.getProperty('myproperty')
}
// in your tests use it like:
@BeforeAll
fun setUp() {
System.getProperty("myproperty")
}
Basicailly this answer.