BAEL-4742-Guide-To-System-Stubs

This commit is contained in:
Ashley Frieze 2020-11-20 17:44:42 +00:00
parent 683215b2fe
commit 390b864ff3
2 changed files with 90 additions and 0 deletions

View File

@ -9,6 +9,7 @@
<groupId>com.baeldung</groupId>
<artifactId>testing-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<dependencies>
@ -24,6 +25,44 @@
<version>${system-lambda.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>${system-stubs.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-junit4</artifactId>
<version>${system-stubs.version}</version>
<scope>test</scope>
</dependency>
<!-- System Stubs requires more up to date JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -39,5 +78,7 @@
<properties>
<system-rules.version>1.19.0</system-rules.version>
<system-lambda.version>1.0.0</system-lambda.version>
<system-stubs.version>1.0.0</system-stubs.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
</properties>
</project>

View File

@ -0,0 +1,49 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;
import static com.github.stefanbirkner.systemlambda.SystemLambda.restoreSystemProperties;
import static com.github.stefanbirkner.systemlambda.SystemLambda.withEnvironmentVariable;
import static org.junit.Assert.assertEquals;
@ExtendWith(SystemStubsExtension.class)
class SystemLambdaComparisonUnitTest {
@SystemStub
private EnvironmentVariables environmentVariables =
new EnvironmentVariables("ADDRESS", "https://www.baeldung.com");
@SystemStub
private SystemProperties systemProperties = new SystemProperties();
@Test
void aSingleSystemLambda() throws Exception {
restoreSystemProperties(() -> {
System.setProperty("log_dir", "test/resources");
assertEquals("test/resources", System.getProperty("log_dir"));
});
}
@Test
void multipleSystemLambdas() throws Exception {
restoreSystemProperties(() -> {
withEnvironmentVariable("URL", "https://www.baeldung.com")
.execute(() -> {
System.setProperty("log_dir", "test/resources");
assertEquals("test/resources", System.getProperty("log_dir"));
assertEquals("https://www.baeldung.com", System.getenv("URL"));
});
});
}
@Test
void multipleSystemStubs() {
System.setProperty("log_dir", "test/resources");
assertEquals("test/resources", System.getProperty("log_dir"));
assertEquals("https://www.baeldung.com", System.getenv("ADDRESS"));
}
}