BAEL-6585 - Unit Testing of System.in with JUnit (#14436)
* BAEL-6585 - Unit Testing of System.in with JUnit * Rename test classes * Remane test classes
This commit is contained in:
parent
6443fc6509
commit
9b74111078
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.systemin;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static final String NAME = "Name: ";
|
||||
|
||||
private Application() {
|
||||
}
|
||||
|
||||
public static String readName() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String input = scanner.next();
|
||||
return NAME.concat(input);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.systemin;
|
||||
|
||||
import com.github.stefanbirkner.systemlambda.SystemLambda;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import uk.org.webcompere.systemstubs.SystemStubs;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
|
||||
import static com.baeldung.systemin.Application.NAME;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class SystemInUnitTest {
|
||||
private void provideInput(String data) {
|
||||
ByteArrayInputStream testIn = new ByteArrayInputStream(data.getBytes());
|
||||
System.setIn(testIn);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenName_whenReadNameFromInput_thenReturnCorrectResult() {
|
||||
provideInput("Baeldung");
|
||||
String input = Application.readName();
|
||||
assertEquals(NAME.concat("Baeldung"), input);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenName_whenReadWithSystemLambda_thenReturnCorrectResult() throws Exception {
|
||||
SystemLambda.withTextFromSystemIn("Baeldung")
|
||||
.execute(() -> assertEquals(NAME.concat("Baeldung"), Application.readName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenName_whenReadWithSystemStubs_thenReturnCorrectResult() throws Exception {
|
||||
SystemStubs.withTextFromSystemIn("Baeldung")
|
||||
.execute(() -> {
|
||||
assertThat(Application.readName())
|
||||
.isEqualTo(NAME.concat("Baeldung"));
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.systemin;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.contrib.java.lang.system.TextFromStandardInputStream;
|
||||
|
||||
import static com.baeldung.systemin.Application.NAME;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.contrib.java.lang.system.TextFromStandardInputStream.emptyStandardInputStream;
|
||||
|
||||
public class SystemRulesUnitTest {
|
||||
|
||||
@Rule
|
||||
public final TextFromStandardInputStream systemIn = emptyStandardInputStream();
|
||||
|
||||
@Test
|
||||
public void givenName_whenReadWithSystemRules_thenReturnCorrectResult() {
|
||||
systemIn.provideLines("Baeldung");
|
||||
assertEquals(NAME.concat("Baeldung"), Application.readName());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.systemin;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import uk.org.webcompere.systemstubs.rules.SystemInRule;
|
||||
|
||||
import static com.baeldung.systemin.Application.NAME;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SystemStubsUnitTest {
|
||||
|
||||
@Rule
|
||||
public SystemInRule systemInRule = new SystemInRule("Baeldung");
|
||||
|
||||
@Test
|
||||
public void givenName_whenReadWithSystemStubs_thenReturnCorrectResult() {
|
||||
assertThat(Application.readName()).isEqualTo(NAME.concat("Baeldung"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue