Advertisement
JeffGrigg

Untitled

Apr 2nd, 2021
2,270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.34 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.     private static final String NL = System.lineSeparator();
  9.  
  10.     public void testHelloWorld_NoCommandLineParamaters() {
  11.         assertEquals("Hello world!" + NL, callMainMethod(new String[]{}));
  12.     }
  13.  
  14.     public void testHelloJeff() {
  15.         assertEquals("Hello Jeff!" + NL, callMainMethod(new String[]{"Jeff"}));
  16.     }
  17.  
  18.     public void testHelloAlan() {
  19.         assertEquals("Hello Alan!" + NL, callMainMethod(new String[]{"Alan"}));
  20.     }
  21.  
  22.     public void testHelloAlanHolub() {
  23.         assertEquals("Hello Alan Holub!" + NL, callMainMethod(new String[]{"Alan", "Holub"}));
  24.     }
  25.  
  26.     public void test9parms() {
  27.         assertEquals(
  28.                 "Hello 1 2 3 4 5 6 7 8 9!" + NL,
  29.                 callMainMethod(new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9"}));
  30.     }
  31.  
  32.     private static String callMainMethod(final String[] args) {
  33.         final var oldSystemOut = System.out;
  34.         final var outputStream = new ByteArrayOutputStream();
  35.         System.setOut(new PrintStream(outputStream));
  36.         try {
  37.             HelloWorld.main(args);
  38.         } finally {
  39.             System.setOut(oldSystemOut);
  40.         }
  41.         return outputStream.toString();
  42.     }
  43.  
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement