question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SetEnvironmentVariable and SetSystemProperty annotation cannot work in Junit 5

See original GitHub issue

I write below test case, but it cannot work. Please help check it.

@Test
@SetEnvironmentVariable(key = "abc", value = "123")
public void testHandleRequest() {
    // AlertAPIHandler apiHandler = new AlertAPIHandler();
    assertEquals("123", System.getenv("abc"));
}

@Test
@SetSystemProperty(key = "Test", value = "123")
public void testSystemProperty() {
    assertEquals("123", System.getProperty("Test"));
}

Below is my Gradle Settings:

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
testCompile group: 'org.junit-pioneer', name: 'junit-pioneer', version: '1.4.2'

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
scgerkincommented, Aug 17, 2021

Downgrading JUnit to 5.7.2 fixed the issue for me.

0reactions
beatngu13commented, Dec 3, 2021

Running the following test:

class Issue508 {

	@Test
	@SetEnvironmentVariable(key = "env", value = "var")
	void testEnvVar() {
		assertEquals("var", System.getenv("env"));
	}

	@Test
	@SetSystemProperty(key = "sys", value = "prop")
	void testSysProp() {
		assertEquals("prop", System.getProperty("sys"));
	}

}

With the following build / Maven config:

<properties>
	<encoding>UTF-8</encoding>
	<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
	<project.resources.sourceEncoding>${encoding}</project.resources.sourceEncoding>
	<java.version>11</java.version>
	<junit.jupiter.version>5.8.2</junit.jupiter.version>
</properties>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.junit</groupId>
			<artifactId>junit-bom</artifactId>
			<version>${junit.jupiter.version}</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<dependencies>
	<dependency>
		<groupId>org.junit.jupiter</groupId>
		<artifactId>junit-jupiter</artifactId>
		<version>${junit.jupiter.version}</version>
		<scope>test</scope>
	</dependency>

	<dependency>
		<groupId>org.junit-pioneer</groupId>
		<artifactId>junit-pioneer</artifactId>
		<version>1.5.0</version>
		<scope>test</scope>
	</dependency>
</dependencies>

<build>
	<plugins>
		<plugin>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.8.1</version>
			<configuration>
				<release>${java.version}</release>
			</configuration>
		</plugin>

		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-plugin</artifactId>
			<version>2.22.2</version>
		</plugin>
	</plugins>
</build>

I wasn’t able to reproduce the errors described above.

Therefore, I will close this issue. Feel free to reopen/comment if the problem pops up again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Properly set (system) properties in JUnit 5 - Stack Overflow
Both annotations work on the test method and class level, are repeatable as well as combinable. After the annotated method has been executed, ......
Read more >
Allow simple expressions in SetEnvironmentVariables ...
would allow the SetEnvironmentVariables/SetSystemProperties annotations to dynamically resolve environment variable values from the runtime ...
Read more >
Guide to the System Stubs Library - Baeldung
Learn the importance of being able to mock system resources and how System Stubs allows for complex configurations of stubbing with JUnit 4 ......
Read more >
JUnit Pioneer 1.0 - nipafx.dev
Yesterday we released JUnit Pioneer 1.0 - here's a quick rundown ... (so i-klingon and xtg-x-cel-gaulish work) and both annotations work ...
Read more >
SetEnvironmentVariable (junit-pioneer 1.5.0 API) - Javadoc.io
SetEnvironmentVariable is repeatable and can be used on the method and on the class level. If a class is annotated, the configured variable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found