BAEL-4742 System Stubs example tests

This commit is contained in:
Ashley Frieze 2020-11-24 23:38:20 +00:00
parent 390b864ff3
commit 3c01b8f9f8
21 changed files with 723 additions and 1 deletions

View File

@ -13,6 +13,12 @@
</parent>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.16.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
@ -78,7 +84,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>
<system-stubs.version>1.1.0</system-stubs.version>
<junit.jupiter.version>5.6.2</junit.jupiter.version>
</properties>
</project>

View File

@ -0,0 +1,16 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SystemStubsExtension.class)
class FakeDatabaseJUnit5UnitTest {
@Test
void useFakeDatabase(FakeDatabaseTestResource fakeDatabase) {
assertThat(fakeDatabase.getDatabaseConnection()).isEqualTo("open");
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.systemstubs;
import uk.org.webcompere.systemstubs.resource.TestResource;
public class FakeDatabaseTestResource implements TestResource {
// let's pretend this is a database connection
private String databaseConnection = "closed";
@Override
public void setup() throws Exception {
databaseConnection = "open";
}
@Override
public void teardown() throws Exception {
databaseConnection = "closed";
}
public String getDatabaseConnection() {
return databaseConnection;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class FakeDatabaseTestResourceUnitTest {
@Nested
class ExecuteAround {
@Test
void theResourceIsClosedToStartWith() throws Exception {
FakeDatabaseTestResource fake = new FakeDatabaseTestResource();
assertThat(fake.getDatabaseConnection()).isEqualTo("closed");
fake.execute(() -> {
assertThat(fake.getDatabaseConnection()).isEqualTo("open");
});
}
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.systemstubs;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import uk.org.webcompere.systemstubs.rules.EnvironmentVariablesRule;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(Enclosed.class)
public class GettingStartedWithSystemStubsJUnit4UnitTest {
public static class SetEnvironmentInsideTest {
@Rule
public EnvironmentVariablesRule environmentVariablesRule = new EnvironmentVariablesRule();
@Test
public void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
environmentVariablesRule.set("ENV", "value1");
assertThat(System.getenv("ENV")).isEqualTo("value1");
}
}
public static class SetEnvironmentAtConstruction {
@Rule
public EnvironmentVariablesRule environmentVariablesRule =
new EnvironmentVariablesRule("ENV", "value1",
"ENV2", "value2");
@Test
public void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
assertThat(System.getenv("ENV")).isEqualTo("value1");
assertThat(System.getenv("ENV2")).isEqualTo("value2");
}
}
public static class SetEnvironmentInBefore {
@Rule
public EnvironmentVariablesRule environmentVariablesRule =
new EnvironmentVariablesRule();
@Before
public void before() {
environmentVariablesRule.set("ENV", "value1")
.set("ENV2", "value2");
}
@Test
public void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
assertThat(System.getenv("ENV")).isEqualTo("value1");
assertThat(System.getenv("ENV2")).isEqualTo("value2");
}
}
}

View File

@ -0,0 +1,109 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Nested;
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 org.assertj.core.api.Assertions.assertThat;
import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariable;
import static uk.org.webcompere.systemstubs.resource.Resources.with;
class GettingStartedWithSystemStubsUnitTest {
@Nested
@ExtendWith(SystemStubsExtension.class)
class EnvironmentVariablesJUnit5 {
@SystemStub
private EnvironmentVariables environmentVariables;
@Test
void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
environmentVariables.set("ENV", "value1");
assertThat(System.getenv("ENV")).isEqualTo("value1");
}
}
@Nested
@ExtendWith(SystemStubsExtension.class)
class EnvironmentVariablesConstructedJUnit5 {
@SystemStub
private EnvironmentVariables environmentVariables =
new EnvironmentVariables("ENV", "value1");
@Test
void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
assertThat(System.getenv("ENV")).isEqualTo("value1");
}
}
@Nested
@ExtendWith(SystemStubsExtension.class)
class EnvironmentVariablesConstructedWithSetJUnit5 {
@SystemStub
private EnvironmentVariables environmentVariables =
new EnvironmentVariables()
.set("ENV", "value1")
.set("ENV2", "value2");
@Test
void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet() {
assertThat(System.getenv("ENV")).isEqualTo("value1");
}
}
@Nested
@ExtendWith(SystemStubsExtension.class)
class EnvironmentVariablesJUnit5ParameterInjection {
@Test
void givenEnvironmentCanBeModified_whenSetEnvironment_thenItIsSet(EnvironmentVariables environmentVariables) {
environmentVariables.set("ENV", "value1");
assertThat(System.getenv("ENV")).isEqualTo("value1");
}
}
@Nested
class EnvironmentVariablesExecuteAround {
@Test
void givenSetupUsingWithEnvironmentVariable_thenItIsSet() throws Exception {
withEnvironmentVariable("ENV3", "val")
.execute(() -> {
assertThat(System.getenv("ENV3")).isEqualTo("val");
});
}
@Test
void givenSetupUsingConstructor_thenItIsSet() throws Exception {
EnvironmentVariables environment = new EnvironmentVariables()
.set("ENV3", "val");
environment.execute(() -> {
assertThat(System.getenv("ENV3")).isEqualTo("val");
});
}
@Test
void givenEnvironment_thenCanReturnValue() throws Exception {
String extracted = new EnvironmentVariables("PROXY", "none")
.execute(() -> System.getenv("PROXY"));
assertThat(extracted).isEqualTo("none");
}
}
@Nested
class RunMultiple {
@Test
void runMultiple() throws Exception {
with(new EnvironmentVariables("FOO", "bar"),
new SystemProperties("prop", "val"))
.execute(() -> {
assertThat(System.getenv("FOO")).isEqualTo("bar");
assertThat(System.getProperty("prop")).isEqualTo("val");
});
}
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.systemstubs;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import uk.org.webcompere.systemstubs.rules.SystemPropertiesRule;
import static org.assertj.core.api.Assertions.assertThat;
public class JUnit4SystemPropertiesUnitTest {
@Rule
public SystemPropertiesRule systemProperties =
new SystemPropertiesRule("db.connection", "false");
@Before
public void before() {
systemProperties.set("before.prop", "before");
}
@Test
public void givenPropertyIsSet_thenCanBeUsedInTest() {
assertThat(System.getProperty("db.connection")).isEqualTo("false");
}
@Test
public void givenPropertyIsSet_thenAnotherCanBeSetAndBeUsedInTest() {
assertThat(System.getProperty("db.connection")).isEqualTo("false");
systemProperties.set("prop2", "true");
assertThat(System.getProperty("prop2")).isEqualTo("true");
}
@Test
public void givenPropertySetInBefore_thenCanBeSeenInTest() {
assertThat(System.getProperty("before.prop")).isEqualTo("before");
}
@Test
public void givenPropertySetEarlier_thenNotVisibleLater() {
assertThat(System.getProperty("prop2")).isNull();
}
}

View File

@ -0,0 +1,89 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.properties.SystemProperties;
import uk.org.webcompere.systemstubs.resource.PropertySource;
import static org.assertj.core.api.Assertions.assertThat;
class JUnit5SystemPropertiesUnitTest {
@ExtendWith(SystemStubsExtension.class)
@Nested
class RestoreSystemProperties {
@SystemStub
private SystemProperties systemProperties;
@Test
void givenAPropertyIsSet_thenItIsOnlyAvailableInsideThisTest1() {
assertThat(System.getProperty("localProperty")).isNull();
System.setProperty("localProperty", "nonnull");
assertThat(System.getProperty("localProperty")).isEqualTo("nonnull");
}
@Test
void givenAPropertyIsSet_thenItIsOnlyAvailableInsideThisTest2() {
assertThat(System.getProperty("localProperty")).isNull();
System.setProperty("localProperty", "true");
assertThat(System.getProperty("localProperty")).isEqualTo("true");
}
}
@ExtendWith(SystemStubsExtension.class)
@Nested
class RestoreSystemPropertiesByParameter {
@Test
void givenAPropertyIsSet_thenItIsOnlyAvailableInsideThisTest1(SystemProperties systemProperties) {
assertThat(System.getProperty("localProperty")).isNull();
System.setProperty("localProperty", "nonnull");
assertThat(System.getProperty("localProperty")).isEqualTo("nonnull");
}
@Test
void givenAPropertyIsSet_thenItIsOnlyAvailableInsideThisTest2(SystemProperties systemProperties) {
assertThat(System.getProperty("localProperty")).isNull();
System.setProperty("localProperty", "true");
assertThat(System.getProperty("localProperty")).isEqualTo("true");
}
}
@ExtendWith(SystemStubsExtension.class)
@Nested
class SetSomeSystemProperties {
@SystemStub
private SystemProperties systemProperties;
@BeforeEach
void before() {
systemProperties.set("beforeProperty", "before");
}
@Test
void givenAPropertyIsSetInBefore_thenItIsAvailableInsideThisTest() {
assertThat(System.getProperty("beforeProperty")).isEqualTo("before");
}
}
@ExtendWith(SystemStubsExtension.class)
@Nested
class SetSomeSystemPropertiesFromResources {
@SystemStub
private SystemProperties systemProperties =
new SystemProperties(PropertySource.fromResource("test.properties"));
@Test
void givenPropertiesReadFromResources_thenCanBeUsed() {
assertThat(System.getProperty("name")).isEqualTo("baeldung");
}
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.systemstubs;
import org.junit.Rule;
import org.junit.Test;
import uk.org.webcompere.systemstubs.rules.SystemOutRule;
import uk.org.webcompere.systemstubs.stream.output.NoopStream;
public class OutputMutingJUnit4UnitTest {
@Rule
public SystemOutRule systemOutRule = new SystemOutRule(new NoopStream());
@Test
public void givenMuteSystemOut() throws Exception {
System.out.println("nothing is output");
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.stream.SystemOut;
import uk.org.webcompere.systemstubs.stream.output.NoopStream;
import static uk.org.webcompere.systemstubs.SystemStubs.muteSystemOut;
class OutputMutingUnitTest {
@Nested
class MutingWithFacade {
@Test
void givenMuteSystemOut() throws Exception {
muteSystemOut(() -> {
System.out.println("nothing is output");
});
}
}
@ExtendWith(SystemStubsExtension.class)
@Nested
class MutingWithJUnit5 {
@SystemStub
private SystemOut systemOut = new SystemOut(new NoopStream());
@Test
void givenMuteSystemOut() throws Exception {
System.out.println("nothing is output");
}
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static uk.org.webcompere.systemstubs.SystemStubs.catchSystemExit;
class SystemExitExecuteAroundUnitTest {
@Test
void canCheckExitCode() throws Exception {
int exitCode = catchSystemExit(() -> {
System.exit(123);
});
assertThat(exitCode).isEqualTo(123);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.systemstubs;
import org.junit.Rule;
import org.junit.Test;
import uk.org.webcompere.systemstubs.rules.SystemExitRule;
import uk.org.webcompere.systemstubs.security.AbortExecutionException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class SystemExitJUnit4UnitTest {
@Rule
public SystemExitRule systemExitRule = new SystemExitRule();
@Test
public void whenAccidentalSystemExit_thenTestFailsRatherThanJVMKilled() {
// uncomment this to try it
//System.exit(1);
}
@Test
public void whenExit_thenExitCodeIsAvailable() {
assertThatThrownBy(() -> {
System.exit(123);
}).isInstanceOf(AbortExecutionException.class);
assertThat(systemExitRule.getExitCode()).isEqualTo(123);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.security.AbortExecutionException;
import uk.org.webcompere.systemstubs.security.SystemExit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@ExtendWith(SystemStubsExtension.class)
class SystemExitJUnit5UnitTest {
@SystemStub
private SystemExit systemExit;
@Test
void whenExit_thenExitCodeIsAvailable() {
assertThatThrownBy(() -> {
System.exit(123);
}).isInstanceOf(AbortExecutionException.class);
assertThat(systemExit.getExitCode()).isEqualTo(123);
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import java.util.Scanner;
import static org.assertj.core.api.Assertions.assertThat;
import static uk.org.webcompere.systemstubs.SystemStubs.withTextFromSystemIn;
class SystemInExecuteAroundUnitTest {
@Test
void givenTextInSystemIn_thenCanReadIt() throws Exception {
withTextFromSystemIn("line1", "line2", "line3")
.execute(() -> {
assertThat(new Scanner(System.in).nextLine())
.isEqualTo("line1");
});
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.systemstubs;
import org.junit.Rule;
import org.junit.Test;
import uk.org.webcompere.systemstubs.rules.SystemInRule;
import java.util.Scanner;
import static org.assertj.core.api.Assertions.assertThat;
public class SystemInJUnit4UnitTest {
@Rule
public SystemInRule systemInRule =
new SystemInRule("line1", "line2", "line3");
@Test
public void givenInput_canReadFirstLine() {
assertThat(new Scanner(System.in).nextLine())
.isEqualTo("line1");
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.stream.SystemIn;
import java.util.Scanner;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SystemStubsExtension.class)
class SystemInJUnit5UnitTest {
@SystemStub
private SystemIn systemIn = new SystemIn("line1", "line2", "line3");
@Test
void givenInput_canReadFirstLine() {
assertThat(new Scanner(System.in).nextLine())
.isEqualTo("line1");
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import uk.org.webcompere.systemstubs.properties.SystemProperties;
import uk.org.webcompere.systemstubs.stream.SystemOut;
import uk.org.webcompere.systemstubs.stream.output.DisallowWriteStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static uk.org.webcompere.systemstubs.SystemStubs.tapSystemOutNormalized;
import static uk.org.webcompere.systemstubs.resource.Resources.with;
class SystemOutAndErrExecuteAroundUnitTest {
@Test
void givenTapOutput_thenGetOutput() throws Exception {
String output = tapSystemOutNormalized(() -> {
System.out.println("a");
System.out.println("b");
});
assertThat(output).isEqualTo("a\nb\n");
}
@Test
void givenCaptureOutputWithSystemOut_thenGetOutput() throws Exception {
SystemOut systemOut = new SystemOut();
SystemProperties systemProperties = new SystemProperties("a", "!");
with(systemOut, systemProperties)
.execute(() -> {
System.out.println("a: " + System.getProperty("a"));
});
assertThat(systemOut.getLines()).containsExactly("a: !");
}
@Test
void givenCannotWrite_thenWritingIsError() {
assertThatThrownBy(() -> {
new SystemOut(new DisallowWriteStream())
.execute(() -> System.out.println("boo"));
}).isInstanceOf(AssertionError.class);
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.systemstubs;
import org.junit.Rule;
import org.junit.Test;
import uk.org.webcompere.systemstubs.rules.SystemErrRule;
import uk.org.webcompere.systemstubs.rules.SystemOutRule;
import static org.assertj.core.api.Assertions.assertThat;
public class SystemOutJUnit4UnitTest {
@Rule
public SystemOutRule systemOutRule = new SystemOutRule();
@Rule
public SystemErrRule systemErrRule = new SystemErrRule();
@Test
public void whenCodeWritesToSystemOut_itCanBeRead() {
System.out.println("line1");
System.out.println("line2");
assertThat(systemOutRule.getLines())
.containsExactly("line1", "line2");
}
@Test
public void whenCodeWritesToSystemOut_itCanBeReadAsText() {
System.out.println("line1");
System.out.println("line2");
assertThat(systemOutRule.getText())
.startsWith("line1");
}
@Test
public void whenCodeWritesToSystemOut_itCanBeReadAsNormalizedLines() {
System.out.println("line1");
System.out.println("line2");
assertThat(systemOutRule.getLinesNormalized())
.isEqualTo("line1\nline2\n");
}
@Test
public void whenCodeWritesToSystemErr_itCanBeRead() {
System.err.println("line1");
System.err.println("line2");
assertThat(systemErrRule.getLines())
.containsExactly("line1", "line2");
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.stream.SystemErr;
import uk.org.webcompere.systemstubs.stream.SystemOut;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SystemStubsExtension.class)
class SystemOutJUnit5UnitTest {
@SystemStub
private SystemOut systemOut;
@SystemStub
private SystemErr systemErr;
@Test
void whenWriteToOutput_thenItCanBeAsserted() {
System.out.println("to out");
System.err.println("to err");
assertThat(systemOut.getLines()).containsExactly("to out");
assertThat(systemErr.getLines()).containsExactly("to err");
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import uk.org.webcompere.systemstubs.properties.SystemProperties;
import static org.assertj.core.api.Assertions.assertThat;
import static uk.org.webcompere.systemstubs.SystemStubs.restoreSystemProperties;
class SystemPropertiesExecuteAroundUnitTest {
@Test
void givenRestoreSystemProperties_thenPropertyRestored() throws Exception {
restoreSystemProperties(() -> {
// test code
System.setProperty("unrestored", "true");
});
assertThat(System.getProperty("unrestored")).isNull();
}
@Test
void givenSystemPropertiesObject_thenPropertyRestored() throws Exception {
String result = new SystemProperties()
.execute(() -> {
System.setProperty("unrestored", "true");
return "it works";
});
assertThat(result).isEqualTo("it works");
assertThat(System.getProperty("unrestored")).isNull();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.systemstubs;
import org.junit.jupiter.api.Test;
import uk.org.webcompere.systemstubs.stream.input.LinesAltStream;
import java.util.Scanner;
import static org.assertj.core.api.Assertions.assertThat;
class WithMockedInputStreamUnitTest {
@Test
void givenInputStream_thenCanRead() {
LinesAltStream testInput = new LinesAltStream("line1", "line2");
Scanner scanner = new Scanner(testInput);
assertThat(scanner.nextLine()).isEqualTo("line1");
}
}