System property extension
See original GitHub issueWhile 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:
- Created 5 years ago
- Comments:14 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Back-linked the associated PR #133 and…
…I second this. Let’s get it in, @nicolaiparlog 🚀
I like
@ClearSystemProperty
- very clear, no ambiguity.