Advertisement
JeffGrigg

2nd Test Passes

Feb 8th, 2024
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.12 KB | Software | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.stream.Collectors;
  6.  
  7. public class NBottlesOfBeerOnTheWallTest extends TestCase {
  8.  
  9.     private static String[] lyricsForBottlesOfBeer(final int numberOfBottlesOfBeer) {
  10.         final var lines = new ArrayList<String>();
  11.         if (numberOfBottlesOfBeer == 1) {   // [Yes, this condition is intentionally wrong.]
  12.             lines.add("1 bottle of beer on the wall, 1 bottle of beer.");
  13.             lines.add("Take one down and pass it around, no more bottles of beer on the wall.");
  14.             lines.add("");
  15.         }
  16.         lines.add("No more bottles of beer on the wall, no more bottles of beer.");
  17.         lines.add("Go to the store and buy some more, 99 bottles of beer on the wall.");
  18.         return lines.toArray(new String[lines.size()]);
  19.     }
  20.  
  21.     public void testZeroBottlesOfBeer() {
  22.         assertEqualsArrayValues(new String[]{
  23.                         "No more bottles of beer on the wall, no more bottles of beer.",
  24.                         "Go to the store and buy some more, 99 bottles of beer on the wall."},
  25.                 lyricsForBottlesOfBeer(0));
  26.     }
  27.  
  28.     public void testOneBottleOfBeer() {
  29.         assertEqualsArrayValues(new String[]{
  30.                         "1 bottle of beer on the wall, 1 bottle of beer.",
  31.                         "Take one down and pass it around, no more bottles of beer on the wall.",
  32.                         "",
  33.                         "No more bottles of beer on the wall, no more bottles of beer.",
  34.                         "Go to the store and buy some more, 99 bottles of beer on the wall."
  35.                 },
  36.                 lyricsForBottlesOfBeer(1));
  37.     }
  38.  
  39.     private static void assertEqualsArrayValues(final String[] expectedLines, final String[] actualLines) {
  40.         final var expectedAsString = Arrays.stream(expectedLines).collect(Collectors.joining(System.lineSeparator()));
  41.         final var actualAsString = Arrays.stream(actualLines).collect(Collectors.joining(System.lineSeparator()));
  42.         assertEquals(expectedAsString, actualAsString);
  43.     }
  44.  
  45. }
  46.  
Tags: 99 Bottles
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement