BAEL-4437 - System Rules (#10047)
Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
+19
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -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"));
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+31
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user