/
javapractice
/
JavaPractice
Обзор
Документация
Войти
/
javapractice
/
JavaPractice
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
2
CI/CD
Аналитика
develop
CodeRun/src/test/java/quickStart/QuickStartExercise07Test.java
94 строки
2 KB
Кузьма Даждъбогин
f/TSKJVPRCTC5-13
14 янв 2026, 16:33
14 янв 2026, 16:33
44c488b
Код
Авторство
О чём код?
package quickStart; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.io.*; import static org.junit.jupiter.api.Assertions.assertEquals; class QuickStartExercise07Test { private final InputStream systemIn = System.in; private final PrintStream systemOut = System.out; private ByteArrayInputStream testIn; private ByteArrayOutputStream testOut; @BeforeEach void setUp() { testOut = new ByteArrayOutputStream(); System.setOut(new PrintStream(testOut)); } @AfterEach void tearDown() { System.setIn(systemIn); System.setOut(systemOut); } void provideInput(String data) { testIn = new ByteArrayInputStream(data.getBytes()); System.setIn(testIn); } String getOutput() { return testOut.toString().trim(); } @Test void main_shouldCountDistinctWords_WhenInputHasMultipleSpacesAndLines() throws Exception { // Given String input = "She sells sea shells on the sea shore;\n" + "The shells that she sells are sea shells I'm sure.\n" + "So if she sells sea shells on the sea shore,\n" + "I'm sure that the shells are sea shore shells.\n"; provideInput(input); // When QuickStartExercise07.main(new String[]{}); // Then assertEquals("19", getOutput()); } @Test void main_shouldReturnZero_WhenInputIsEmpty() throws Exception { // Given String input = ""; provideInput(input); // When QuickStartExercise07.main(new String[]{}); // Then assertEquals("0", getOutput()); } @Test void main_shouldHandleSingleWord() throws Exception { // Given String input = "hello"; provideInput(input); // When QuickStartExercise07.main(new String[]{}); // Then assertEquals("1", getOutput()); } @Test void main_shouldIgnoreExtraWhitespace() throws Exception { // Given String input = " word word \n\n word "; provideInput(input); // When QuickStartExercise07.main(new String[]{}); // Then assertEquals("1", getOutput()); } }