Input / Output Tests
What is your first name?
Jane
What is your last name?
Doe
Hello, Jane Doe!Grading Tests:
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.*;
public class CodingRoomsUnitTests {
private final PrintStream standardOut = System.out;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
@Before
public void setUp() {
System.setOut(new PrintStream(outputStreamCaptor));
}
@After
public void tearDown() {
System.setOut(standardOut);
}
@Test
public void testCase1() {
String input = "Joe\nMazzone";
InputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
Example.main(null);
assertTrue(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "").endsWith("Hello Joe Mazzone!".toLowerCase().trim().replace(" ", "")));
}
@Test
public void testCase2() {
String input = "Clark\nKent";
InputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
Example.main(null);
assertTrue(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "").endsWith("Hello Clark Kent!".toLowerCase().trim().replace(" ", "")));
}
@Test
public void testCase3() {
String input = "Bruce\nWayne";
InputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
Example.main(null);
assertTrue(outputStreamCaptor.toString().toLowerCase().trim().replace(" ", "").endsWith("Hello Bruce Wayne!".toLowerCase().trim().replace(" ", "")));
}
}Sample Solution:
Last updated
Was this helpful?