Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static org.junit.jupiter.api.Assertions.*;
- import org.junit.jupiter.api.AfterEach;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.Test;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- import java.io.InputStream;
- /**
- * The test class AggregatedClassTest.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class AggregatedClassTest {
- private final InputStream systemIn = System.in;
- private final PrintStream systemOut = System.out;
- private ByteArrayInputStream testIn;
- private ByteArrayOutputStream testOut;
- /**
- * Default constructor for test class AggregatedClassTest
- */
- public AggregatedClassTest()
- {
- }
- /**
- * Sets up the test environment. Is called before every test case method.
- *
- */
- @BeforeEach
- public void setUp() {
- testOut = new ByteArrayOutputStream();
- System.setOut(new PrintStream(testOut));
- }
- /**
- * Tears down the test environment. Is called after every test case method.
- */
- @AfterEach
- public void tearDown() {
- System.setIn(System.in); //restores the standard in to the console.
- }
- /**
- * block1
- * Creates a ByteArrayOutputStream which enables formating an [output]
- * text stream as a array of bytes.
- * Creates a PrintStream (think of this as like a bus, but for text),
- * with the ByteArrayOutputStream as an argument.
- * Redirects the standard out to the PrintStream. The System class has a
- * method 'public static void setOut(PrintStream out)', that will allows
- * reassignment of the standard output stream.
- *
- * block2
- * Calls the method to be tested.
- * Creates a String[] to capture the output from the
- * ByteArrayOutputStream, splitting the lines on line separators (the EOL
- * character; as from the Enter key).
- *
- * block3
- * Extracts the last line from the array, that is the only one we need;
- * the method outputs a blabk line before the line of text.
- * will represent the standard out.
- * With standard in reassigned, the same is done for standard out.
- * A string that represents the expected result is created.
- *
- * block3
- * Creates the expected string that the method to be tested will output.
- * Performs the test by comparing the redirected standard output to the
- * expected string.
- */
- @Test
- public void testStandardOutput() {
- AggregatedClass testObj = new AggregatedClass();
- //block1
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- PrintStream printStream = new PrintStream(baos);
- System.setOut(printStream);
- //block2
- testObj.standardOutput();
- String[] lines = baos.toString().split(System.lineSeparator());
- String actual = lines[lines.length -1];
- //block3
- String expected = "You wanted to see standard output.";
- assertEquals(expected, actual);
- }
- /**
- * Tests that user input (via standard input stream)
- *
- * block 1
- * Creates an object of type InputStream so you can reassign standard in.
- * There is a class ByteArrayInputStream that will work, because one of
- * the available constructors will take a byte array.
- * The String class' getBytes() method is used to turn the String into a
- * byte[].
- * The standard in stream is redirected to the new ByteArrayInputStream.
- *
- *
- */
- @Test
- public void standardInputAndOutput() {
- //block 1
- //String simulatedOutput = String.format("%sYou wanted to see standard output.", System.lineSeparator());
- //ByteArrayInputStream bais = new ByteArrayInputStream(simulatedOutput.getBytes());
- //System.setIn(bais);
- }
- }
Add Comment
Please, Sign In to add comment