stefanbirkner / system-rules

A collection of JUnit rules for testing code which uses java.lang.System.
http://stefanbirkner.github.io/system-rules
Other
546 stars 71 forks source link

Environment variable is not set by EnvironmentVariables rule #63

Closed debuggins closed 6 years ago

debuggins commented 6 years ago

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:

package com.my.package

import org.junit.Test
import org.junit.Rule
import org.junit.contrib.java.lang.system.EnvironmentVariables

public class MyTest extends GroovyTestCase {
  @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"));
  }
}

Below is the stacktrace for my failing test:

 com.my.package.MyTest > testSetEnvironmentVariable FAILED
    junit.framework.ComparisonFailure: expected:<my/custom/path> but was:<null>
        at junit.framework.Assert.assertEquals(Assert.java:100)
        at junit.framework.TestCase.assertEquals(TestCase.java:261)
        at groovy.util.GroovyTestCase.assertEquals(GroovyTestCase.java:283)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
        at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1467)
        at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:65)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:214)
        at com.my.package.MyTest.testSetEnvironmentVariable(MyTest.groovy:14)

Any help to get this working would be greatly appreciated.

stefanbirkner commented 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.

debuggins commented 6 years ago

Thanks for your quick and thorough response! That worked!

stefanbirkner commented 6 years ago

You're welcome.