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
Oct 2023, 03.

 

My next events:
🗓 Apr 04 16:30 JDevSummitIL Getting Started with the Micronaut Framework