System Stubs

System Stubs is used to test code which depends on methods in java.lang.System.

@ExtendWith(SystemStubsExtension.class)
class WithEnvironmentVariables {
	@SystemStub
	private EnvironmentVariables variables = new EnvironmentVariables("input", "foo");

	@Test
	void hasAccessToEnvironmentVariables() {
		assertThat(System.getenv("input")).isEqualTo("foo");
	}

	@Test
	void changeEnvironmentVariablesDuringTest() {
		variables.set("input", "bar");
		assertThat(System.getenv("input")).isEqualTo("bar");
	}
}

Go to the linked site

Tags: #java #junit5 #test