BAEL-4437 - System Rules (#10047)
Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
parent
4c39bcd6be
commit
f6adcd0cb3
|
@ -41,7 +41,8 @@
|
|||
<module>junit-5-advanced</module>
|
||||
<module>xmlunit-2</module>
|
||||
<module>junit-4</module>
|
||||
<module>testing-libraries</module>
|
||||
<module>testing-libraries</module>
|
||||
<module>testing-libraries-2</module>
|
||||
<module>powermock</module>
|
||||
</modules>
|
||||
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>testing-libraries-2</artifactId>
|
||||
<name>testing-libraries-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>testing-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.stefanbirkner</groupId>
|
||||
<artifactId>system-rules</artifactId>
|
||||
<version>${system-rules.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.stefanbirkner</groupId>
|
||||
<artifactId>system-lambda</artifactId>
|
||||
<version>${system-lambda.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>testing-libraries</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/test/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<system-rules.version>1.19.0</system-rules.version>
|
||||
<system-lambda.version>1.0.0</system-lambda.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.systemrules;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.ClearSystemProperties;
|
||||
|
||||
public class ClearSystemPropertiesWithRuleUnitTest {
|
||||
|
||||
@Rule
|
||||
public final ClearSystemProperties userNameIsClearedRule = new ClearSystemProperties("user.name");
|
||||
|
||||
@Test
|
||||
public void givenClearUsernameProperty_whenGetUserName_thenNull() {
|
||||
assertNull(System.getProperty("user.name"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.systemrules;
|
||||
|
||||
import static com.github.stefanbirkner.systemlambda.SystemLambda.restoreSystemProperties;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ProvidesSystemPropertyUnitTest {
|
||||
|
||||
@BeforeAll
|
||||
static void setUpBeforeClass() throws Exception {
|
||||
System.setProperty("log_dir", "/tmp/baeldung/logs");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSetSystemProperty_whenGetLogDir_thenLogDirIsProvidedSuccessfully() throws Exception {
|
||||
restoreSystemProperties(() -> {
|
||||
System.setProperty("log_dir", "test/resources");
|
||||
assertEquals("log_dir should be provided", "test/resources", System.getProperty("log_dir"));
|
||||
});
|
||||
|
||||
assertEquals("log_dir should be provided", "/tmp/baeldung/logs", System.getProperty("log_dir"));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.systemrules;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
|
||||
|
||||
public class ProvidesSystemPropertyWithRuleUnitTest {
|
||||
|
||||
@Rule
|
||||
public final ProvideSystemProperty providesSystemPropertyRule = new ProvideSystemProperty("log_dir", "test/resources").and("another_property", "another_value");
|
||||
|
||||
@Rule
|
||||
public final ProvideSystemProperty providesSystemPropertyFromFileRule = ProvideSystemProperty.fromResource("/test.properties");
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() {
|
||||
setLogs();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterClass() throws Exception {
|
||||
System.out.println(System.getProperty("log_dir"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProvideSystemProperty_whenGetLogDir_thenLogDirIsProvidedSuccessfully() {
|
||||
assertEquals("log_dir should be provided", "test/resources", System.getProperty("log_dir"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenProvideSystemPropertyFromFile_whenGetName_thenNameIsProvidedSuccessfully() {
|
||||
assertEquals("name should be provided", "baeldung", System.getProperty("name"));
|
||||
assertEquals("version should be provided", "1.0", System.getProperty("version"));
|
||||
}
|
||||
|
||||
private static void setLogs() {
|
||||
System.setProperty("log_dir", "/tmp/baeldung/logs");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.systemrules;
|
||||
|
||||
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErr;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SystemErrPrintlnUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTapSystemErr_whenInvokePrintln_thenOutputIsReturnedSuccessfully() throws Exception {
|
||||
|
||||
String text = tapSystemErr(() -> {
|
||||
printError("An error occurred Baeldung Readers!!");
|
||||
});
|
||||
|
||||
Assert.assertEquals("An error occurred Baeldung Readers!!", text.trim());
|
||||
}
|
||||
|
||||
private void printError(String output) {
|
||||
System.err.println(output);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.systemrules;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.SystemErrRule;
|
||||
|
||||
public class SystemErrPrintlnWithRuleUnitTest {
|
||||
|
||||
@Rule
|
||||
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog();
|
||||
|
||||
@Test
|
||||
public void givenSystemErrRule_whenInvokePrintln_thenLogSuccess() {
|
||||
printError("An Error occurred Baeldung Readers!!");
|
||||
|
||||
Assert.assertEquals("An Error occurred Baeldung Readers!!", systemErrRule.getLog()
|
||||
.trim());
|
||||
}
|
||||
|
||||
private void printError(String output) {
|
||||
System.err.println(output);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.systemrules;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import static com.github.stefanbirkner.systemlambda.SystemLambda.catchSystemExit;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SystemExitUnitTest {
|
||||
|
||||
@Test
|
||||
void givenCatchSystemExit_whenAppCallsSystemExit_thenStatusIsReturnedSuccessfully() throws Exception {
|
||||
int statusCode = catchSystemExit(() -> {
|
||||
exit();
|
||||
});
|
||||
assertEquals("status code should be 1:", 1, statusCode);
|
||||
}
|
||||
|
||||
private void exit() {
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.systemrules;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.ExpectedSystemExit;
|
||||
|
||||
public class SystemExitWithRuleUnitTest {
|
||||
|
||||
@Rule
|
||||
public final ExpectedSystemExit exitRule = ExpectedSystemExit.none();
|
||||
|
||||
@Test
|
||||
public void givenSystemExitRule_whenAppCallsSystemExit_thenExitRuleWorkssAsExpected() {
|
||||
exitRule.expectSystemExitWithStatus(1);
|
||||
exit();
|
||||
}
|
||||
|
||||
private void exit() {
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.systemrules;
|
||||
|
||||
import static com.github.stefanbirkner.systemlambda.SystemLambda.withTextFromSystemIn;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SystemInUnitTest {
|
||||
|
||||
@Test
|
||||
void givenTwoNames_whenSystemInMock_thenNamesJoinedTogether() throws Exception {
|
||||
withTextFromSystemIn("Jonathan", "Cook").execute(() -> {
|
||||
assertEquals("Names should be concatenated", "Jonathan Cook", getFullname());
|
||||
});
|
||||
}
|
||||
|
||||
private String getFullname() {
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
String firstName = scanner.next();
|
||||
String surname = scanner.next();
|
||||
return String.join(" ", firstName, surname);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.systemrules;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.TextFromStandardInputStream;
|
||||
import static org.junit.contrib.java.lang.system.TextFromStandardInputStream.emptyStandardInputStream;
|
||||
|
||||
public class SystemInWithRuleUnitTest {
|
||||
|
||||
@Rule
|
||||
public final TextFromStandardInputStream systemInMock = emptyStandardInputStream();
|
||||
|
||||
@Test
|
||||
public void givenTwoNames_whenSystemInMock_thenNamesJoinedTogether() {
|
||||
systemInMock.provideLines("Jonathan", "Cook");
|
||||
assertEquals("Names should be concatenated", "Jonathan Cook", getFullname());
|
||||
}
|
||||
|
||||
private String getFullname() {
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
String firstName = scanner.next();
|
||||
String surname = scanner.next();
|
||||
return String.join(" ", firstName, surname);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
name=baeldung
|
||||
version=1.0
|
Loading…
Reference in New Issue