System.getProperty cannot retrieve values
Java 1.8
Gradle 2.14
gradle test-Denv=test
Sample.java
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class Sample {
@ Test
public void testHoge(){
String hoge=System.getProperty("env");
assertEquals("test", hoge);
}
}
build.gradle
apply plugin: 'java'
sourceCompatibility=1.8
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
Sample>testHoge FAILED
java.lang.AssertionError: expected:<test>but was:<null>
at org.junit.Assert.fail (Assert.java:88)
at org.junit.Assert.failNotEquals (Assert.java:834)
at org.junit.Assert.assertEquals (Assert.java:118)
at org.junit.Assert.assertEquals (Assert.java:144)
at Sample.testHoge (Sample.java:13)
1 test completed, 1 failed
java gradle
gradle test-Denv=test
means that you specified the system property env=test
for the Gradle process using the gradle command.It does not reach the test Java process launched from the Gradle process.
To pass the same system properties as the Gradle process to the test Java process launched from the Gradle process:This sets all of the Gradle process's own system properties (System.properties
) to the Java process's system properties (systemProperties
) for tasks of type Test
of the Java plug-in.
tasks.withType(Test){
systemProperties System.properties
}
Incidentally, to take over the system properties to the Java process for applications instead of testing:
tasks.withType(JavaExec){
systemProperties System.properties
}
© 2024 OneMinuteCode. All rights reserved.