// // FourthMainTest.java: test FourthSum.main (input and output) // // See FourthSumTest.java for build directions // import org.junit.Test; import static org.junit.Assert.*; import org.junit.Before; import org.junit.After; import org.junit.Test; import static org.junit.Assert.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class FourthMainTest { private ByteArrayOutputStream sysout; private ByteArrayOutputStream syserr; private ByteArrayInputStream sysin; private static final String newLine = System.getProperty("line.separator"); @Before public void setUp() throws Exception { sysout = new ByteArrayOutputStream(); syserr = new ByteArrayOutputStream(); System.setOut(new PrintStream(sysout, true, "UTF8")); System.setErr(new PrintStream(syserr, true, "UTF8")); } @After public void teardown() throws Exception { System.setIn(System.in); System.setOut(System.out); System.setErr(System.err); } @Test public void testSingleNumber() throws Exception { sysin = new ByteArrayInputStream("-11".getBytes("UTF8")); System.setIn(sysin); FourthSum.main(null); String console = sysout.toString(); String expected = "Enter number(s) on one line: " // prompt + "To fourth power: 14641" + newLine; // result assertEquals(expected, console); } @Test public void testList() throws Exception { sysin = new ByteArrayInputStream("-11 5 0 8".getBytes("UTF8")); System.setIn(sysin); FourthSum.main(null); String console = sysout.toString(); int sum = 14641 + 625 + 0 + 4096; String expected = "Enter number(s) on one line: " // prompt + "Sum of x^4: " + sum + newLine; // result assertEquals(expected, console); } }