Closed debuggins closed 6 years ago
Thanks for using System Rules and providing a helpful bug report.
Your test class extends GroovyTestCase
. This means you're running a JUnit 3 test and JUnit 3 does not support rules. (It also does not support @Test
and it only runs your test method because its name starts with "test").
In order to run your test with JUnit 4 you only have to remove the extends GroovyTestCase
:
package com.my.package
import org.junit.Test
import org.junit.Rule
import org.junit.contrib.java.lang.system.EnvironmentVariables
public class MyTest {
@Rule
public final EnvironmentVariables environmentVariables = new EnvironmentVariables()
@Test
public void testSetEnvironmentVariable() {
environmentVariables.set("CUSTOM_PATH", "my/custom/path");
assertEquals("my/custom/path", System.getenv("CUSTOM_PATH"));
}
}
For more details about JUnit and Groovy see Groovy's Core Testing Guide
Can you please tell me whether this fixes your problem. Thanks.
Thanks for your quick and thorough response! That worked!
You're welcome.
I'm trying to set up a simple test case for setting an environment variable and I'm getting a failed test due to the environment variable resulting in null.
Using: System Rules 1.18.0, JUnit 4.11, Gradle 4.4.1, Groovy 2.4.12
Below is the code for my class:
Below is the stacktrace for my failing test:
Any help to get this working would be greatly appreciated.