Advertisement
JeffGrigg

JUnit Hello World

Jul 28th, 2019
894
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.93 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.PrintStream;
  5.  
  6. public class HelloWorldTest extends TestCase {
  7.  
  8.     public static void main(final String[] args) {
  9.         System.out.println("Hello World!");
  10.     }
  11.  
  12.     public void testHelloWorld() {
  13.         // [Arrange] Capture "Standard I/O" System output:
  14.         final PrintStream oldSystemOut = System.out;
  15.         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  16.         final PrintStream printStream = new PrintStream(byteArrayOutputStream);
  17.         System.setOut(printStream);
  18.         try {
  19.  
  20.             // [Act] Code Under Test:
  21.             main(new String[0]);
  22.  
  23.         } finally {
  24.             System.setOut(oldSystemOut);
  25.             printStream.close();
  26.         }
  27.  
  28.         // [Assert]
  29.         assertEquals("Hello World!" + System.lineSeparator(), byteArrayOutputStream.toString());
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement