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.

System property extension

See original GitHub issue

While looking for a way to handle system properties in JUnit 5 tests on Stack Overflow, I came up with the following extension (see this gist) based on the provided answer:

class SystemPropertyExtensionTest {

	@BeforeAll
	static void setUpOnce() {
		System.setProperty( "some property", "old value" );
	}

	@SystemProperty( key = "some property" )
	@Test
	void extension_should_set_property_to_null() {
		Assertions.assertNull( System.getProperty( "some property" ) );
	}

	@SystemProperty( key = "some property", value = "new value" )
	@Test
	void extension_should_set_property_to_value() {
		Assertions.assertEquals( System.getProperty( "some property" ), "new value" );
	}

	@SystemProperty( key = "some property" )
	@SystemProperty( key = "another property", value = "new value" )
	@Test
	void extension_should_be_repeatable() {
		Assertions.assertNull( System.getProperty( "some property" ) );
		Assertions.assertEquals( System.getProperty( "another property" ), "new value" );
	}

	@AfterAll
	static void tearDownOnce() {
		Assertions.assertEquals( System.getProperty( "some property" ), "old value" );
	}

}

After posting it on Twitter, @sbrannen drew my attention to JUnit Pioneer. I would be happy to submit a PR, which is why I have opened this question to debate the solution.

IMHO there are currently two minor issues:

  • The annotation probably should be repeatable (as already pointed out on Stack Overflow), in case somebody has to use more than one property. (Adapted gist accordingly.)
  • Since null is not allowed for default values, I have picked "" to see if the property only needs to be cleared or set to a specific value. Are there situations where someone has to set a property to an empty string, where this wouldn’t work anymore?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
sormurascommented, Oct 4, 2019

Back-linked the associated PR #133 and…

So, in the interim, it might make sense to go ahead and get the proposed @SystemProperty support in JUnit Pioneer.

…I second this. Let’s get it in, @nicolaiparlog 🚀

3reactions
nipafxcommented, Nov 1, 2018

I like @ClearSystemProperty - very clear, no ambiguity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SystemPropertyExtension - JUnit-Extensions Documentation
JUnit5 extensions library including JUnit5 equivalents of some of the common JUnit4 rules: ExpectedException, TemporaryFolder etc.
Read more >
Properly set (system) properties in JUnit 5 - Stack Overflow
There is JUnit Pioneer, a "JUnit 5 extension pack". It comes with @ClearSystemProperty and @SetSystemProperty . From the docs:.
Read more >
Handling System Properties in JUnit 5 - DEV Community ‍ ‍
In Java, properties are configuration values that are represented as key-value pairs, usually managed inside a Properties object. System ...
Read more >
Clearing or Setting System Properties - JUnit Pioneer
The system property extension is prepared for that and tests annotated with @ClearSystemProperty or @SetSystemProperty will never execute in parallel (thanks to ...
Read more >
Easily handle Java system properties with JUnit 5 extensions
Easily handle Java system properties with JUnit 5 extensions - SystemProperties.java.
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