earlution

AggregatedClassTest.java

Oct 11th, 2021 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. import static org.junit.jupiter.api.Assertions.*;
  2. import org.junit.jupiter.api.AfterEach;
  3. import org.junit.jupiter.api.BeforeEach;
  4. import org.junit.jupiter.api.Test;
  5. import java.io.ByteArrayInputStream;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.PrintStream;
  8. import java.io.InputStream;
  9.  
  10. /**
  11.  * The test class AggregatedClassTest.
  12.  *
  13.  * @author  (your name)
  14.  * @version (a version number or a date)
  15.  */
  16. public class AggregatedClassTest {
  17.    
  18.     private final InputStream systemIn = System.in;
  19.     private final PrintStream systemOut = System.out;
  20.  
  21.     private ByteArrayInputStream testIn;
  22.     private ByteArrayOutputStream testOut;
  23.    
  24.     /**
  25.      * Default constructor for test class AggregatedClassTest
  26.      */
  27.     public AggregatedClassTest()
  28.     {
  29.     }
  30.  
  31.     /**
  32.      * Sets up the test environment.  Is called before every test case method.
  33.      *
  34.      */
  35.     @BeforeEach
  36.     public void setUp() {
  37.         testOut = new ByteArrayOutputStream();
  38.         System.setOut(new PrintStream(testOut));
  39.     }
  40.  
  41.     /**
  42.      * Tears down the test environment.  Is called after every test case method.
  43.      */
  44.     @AfterEach
  45.     public void tearDown() {
  46.         System.setIn(System.in); //restores the standard in to the console.
  47.     }
  48.    
  49.     /**
  50.      * block1
  51.      * Creates a ByteArrayOutputStream which enables formating an [output]
  52.      * text stream as a array of bytes.
  53.      * Creates a PrintStream (think of this as like a bus, but for text),
  54.      * with the ByteArrayOutputStream as an argument.
  55.      * Redirects the standard out to the PrintStream. The System class has a
  56.      * method 'public static void setOut(PrintStream out)', that will allows
  57.      * reassignment of the standard output stream.
  58.      *
  59.      * block2
  60.      * Calls the method to be tested.
  61.      * Creates a String[] to capture the output from the
  62.      * ByteArrayOutputStream, splitting the lines on line separators (the EOL
  63.      * character; as from the Enter key).
  64.      *
  65.      * block3
  66.      * Extracts the last line from the array, that is the only one we need;
  67.      *  the method outputs a blabk line before the line of text.
  68.      * will represent the standard out.
  69.      * With standard in reassigned, the same is done for standard out.
  70.      * A string that represents the expected result is created.
  71.      *
  72.      * block3
  73.      * Creates the expected string that the method to be tested will output.
  74.      * Performs the test by comparing the redirected standard output to the
  75.      * expected string.
  76.      */
  77.     @Test
  78.     public void testStandardOutput() {
  79.         AggregatedClass testObj = new AggregatedClass();
  80.                
  81.         //block1
  82.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  83.         PrintStream printStream = new PrintStream(baos);
  84.         System.setOut(printStream);
  85.        
  86.         //block2
  87.         testObj.standardOutput();
  88.         String[] lines = baos.toString().split(System.lineSeparator());
  89.         String actual = lines[lines.length -1];
  90.        
  91.         //block3
  92.         String expected = "You wanted to see standard output.";
  93.         assertEquals(expected, actual);
  94.     }
  95.    
  96.     /**
  97.      * Tests that user input (via standard input stream)
  98.      *
  99.      * block 1
  100.      * Creates an object of type InputStream so you can reassign standard in.
  101.      * There is a class ByteArrayInputStream that will work, because one of
  102.      * the available constructors will take a byte array.
  103.      * The String class' getBytes() method is used to turn the String into a
  104.      * byte[].
  105.      * The standard in stream is redirected to the new ByteArrayInputStream.
  106.      *
  107.      *
  108.      */
  109.     @Test
  110.     public void standardInputAndOutput() {
  111.         //block 1
  112.         //String simulatedOutput = String.format("%sYou wanted to see standard output.", System.lineSeparator());
  113.         //ByteArrayInputStream bais = new ByteArrayInputStream(simulatedOutput.getBytes());
  114.         //System.setIn(bais);
  115.     }
  116. }
  117.  
  118.  
  119.  
Add Comment
Please, Sign In to add comment