/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
testing-modules/testing-libraries-2/src/test/java/com/baeldung/systemin/SystemInUnitTest.java
40 строк
1 KB
Ana Peterlić
BAEL-6585 - Unit Testing of System.in with JUnit (#14436)
23 июл 2023, 06:49
23 июл 2023, 06:49
9b74111
Код
Авторство
О чём код?
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")); }); } }