Skip to content

Latest commit

 

History

History
66 lines (51 loc) · 1.9 KB

README.md

File metadata and controls

66 lines (51 loc) · 1.9 KB

build coverage Maven Central

SystemSafe

SystemSafe is a hassle-free JUnit 5 extension that prevents unintended side effects from interactions with java.lang.System in tests.

Installation

Maven

<dependency>
    <groupId>com.github.hellproxy</groupId>
    <artifactId>system-safe</artifactId>
    <version>1.0.0</version>
    <scope>test</scope>
</dependency>

Gradle

testImplementation 'com.github.hellproxy:system-safe:1.0.0'

Usage

You can add the following line to src/test/resources/junit-platform.properties:

junit.jupiter.extensions.autodetection.enabled=true

Or you can add the extension to individual test classes:

@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:

@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.