Advertisement
JeffGrigg

NumberOfBottlesOfBeerOnTheWallTest

Feb 17th, 2020
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.29 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.PrintStream;
  5.  
  6. public class NumberOfBottlesOfBeerOnTheWallTest extends TestCase {
  7.  
  8.     private static final String NL = System.lineSeparator();
  9.  
  10.     public void testZeroBottlesOfBeerOnTheWall() {
  11.         final String actualOutput = captureStandardOutput(() -> NumberOfBottlesOfBeerOnTheWall.singNumberOfBottlesOfBeerOnTheWall(0));
  12.  
  13.         assertEquals(
  14.                 "No bottles of beer" + " on the wall. "
  15.                         + "No bottles of beer. "
  16.                         + "There are no more to pass around. "
  17.                         + "No bottles of beer" + " on the wall." + NL,
  18.                 actualOutput);
  19.     }
  20.  
  21.     public void testOneBottleOfBeerOnTheWall() {
  22.         final String actualOutput = captureStandardOutput(() -> NumberOfBottlesOfBeerOnTheWall.singNumberOfBottlesOfBeerOnTheWall(1));
  23.  
  24.         assertEquals(
  25.                 "1 bottle of beer on the wall. "
  26.                         + "1 bottle of beer. "
  27.                         + "Take it down and pass it around. "
  28.                         + "No bottles of beer" + " on the wall." + NL
  29.                         //
  30.                         + "No bottles of beer" + " on the wall. "
  31.                         + "No bottles of beer. "
  32.                         + "There are no more to pass around. "
  33.                         + "No bottles of beer" + " on the wall." + NL,
  34.                 actualOutput);
  35.     }
  36.  
  37.     public void testTwoBottleOfBeerOnTheWall() {
  38.         final String actualOutput = captureStandardOutput(() -> NumberOfBottlesOfBeerOnTheWall.singNumberOfBottlesOfBeerOnTheWall(2));
  39.  
  40.         assertEquals(
  41.                 "2 bottles of beer" + " on the wall. "
  42.                         + "2 bottles of beer. "
  43.                         + "Take one down and pass it around. "
  44.                         + "1 bottle of beer on the wall." + NL
  45.                         //
  46.                         + "1 bottle of beer on the wall. "
  47.                         + "1 bottle of beer. "
  48.                         + "Take it down and pass it around. "
  49.                         + "No bottles of beer" + " on the wall." + NL
  50.                         //
  51.                         + "No bottles of beer" + " on the wall. "
  52.                         + "No bottles of beer. "
  53.                         + "There are no more to pass around. "
  54.                         + "No bottles of beer" + " on the wall." + NL,
  55.                 actualOutput);
  56.     }
  57.  
  58.     public void testThreeBottleOfBeerOnTheWall() {
  59.         final String actualOutput = captureStandardOutput(() -> NumberOfBottlesOfBeerOnTheWall.singNumberOfBottlesOfBeerOnTheWall(3));
  60.  
  61.         assertEquals(
  62.                 "3 bottles of beer on the wall. "
  63.                         + "3 bottles of beer. "
  64.                         + "Take one down and pass it around. "
  65.                         + "2 bottles of beer on the wall." + NL
  66.                         //
  67.                         + "2 bottles of beer" + " on the wall. "
  68.                         + "2 bottles of beer. "
  69.                         + "Take one down and pass it around. "
  70.                         + "1 bottle of beer on the wall." + NL
  71.                         //
  72.                         + "1 bottle of beer on the wall. "
  73.                         + "1 bottle of beer. "
  74.                         + "Take it down and pass it around. "
  75.                         + "No bottles of beer" + " on the wall." + NL
  76.                         //
  77.                         + "No bottles of beer" + " on the wall. "
  78.                         + "No bottles of beer. "
  79.                         + "There are no more to pass around. "
  80.                         + "No bottles of beer" + " on the wall." + NL,
  81.                 actualOutput);
  82.     }
  83.  
  84.     private String captureStandardOutput(final Runnable callback) {
  85.         final PrintStream oldSystemOut = System.out;
  86.         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  87.         final PrintStream printStream = new PrintStream(byteArrayOutputStream);
  88.         System.setOut(printStream);
  89.         try {
  90.             callback.run();
  91.         } finally {
  92.             System.setOut(oldSystemOut);
  93.             printStream.close();
  94.         }
  95.  
  96.         return byteArrayOutputStream.toString();
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement