https://github.com/hellproxy/system-safe
Set and retrieve Java system properties in your unit tests without any side effects
https://github.com/hellproxy/system-safe
java junit5 testing
Last synced: 6 months ago
JSON representation
Set and retrieve Java system properties in your unit tests without any side effects
- Host: GitHub
- URL: https://github.com/hellproxy/system-safe
- Owner: hellproxy
- Created: 2021-02-23T00:40:00.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-03T15:28:01.000Z (over 5 years ago)
- Last Synced: 2023-06-30T16:46:36.063Z (about 3 years ago)
- Topics: java, junit5, testing
- Language: Java
- Homepage:
- Size: 99.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README


[](https://maven-badges.herokuapp.com/maven-central/com.github.hellproxy/system-safe)
# SystemSafe
SystemSafe is a hassle-free JUnit 5 extension that prevents unintended side effects from interactions with
`java.lang.System` in tests.
## Installation
### Maven
```xml
com.github.hellproxy
system-safe
1.0.0
test
```
### Gradle
```groovy
testImplementation 'com.github.hellproxy:system-safe:1.0.0'
```
## Usage
You can add the following line to `src/test/resources/junit-platform.properties`:
```properties
junit.jupiter.extensions.autodetection.enabled=true
```
Or you can add the extension to individual test classes:
```java
@ExtendWith(SystemSafeExtension.class)
class MyTest {
```
You should now be able to get and set System Properties in your tests as if they were being run in isolation. For
example:
```java
@Execution(CONCURRENT)
class FruitTest {
@Test
void testApple() {
System.setProperty("fruit", "apple");
Thread.sleep(100);
assertThat(System.getProperty("fruit")).isEqualTo("apple"); // will sometimes be "banana" (bad!)
}
@Test
void testBanana() {
System.setProperty("fruit", "banana");
Thread.sleep(100);
assertThat(System.getProperty("fruit")).isEqualTo("banana"); // will sometimes be "apple" (also bad!)
}
}
```
Under normal circumstances, the above test would have a race condition when run in parallel. Running with
`SystemSafeExtension` prevents this by giving each test its own sandboxed set of properties to play with.