Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- public class NumberOfBottlesOfBeerOnTheWallTest extends TestCase {
- private static final String NL = System.lineSeparator();
- public void testZeroBottlesOfBeerOnTheWall() {
- final String actualOutput = captureStandardOutput(() -> NumberOfBottlesOfBeerOnTheWall.singNumberOfBottlesOfBeerOnTheWall(0));
- assertEquals(
- "No bottles of beer" + " on the wall. "
- + "No bottles of beer. "
- + "There are no more to pass around. "
- + "No bottles of beer" + " on the wall." + NL,
- actualOutput);
- }
- public void testOneBottleOfBeerOnTheWall() {
- final String actualOutput = captureStandardOutput(() -> NumberOfBottlesOfBeerOnTheWall.singNumberOfBottlesOfBeerOnTheWall(1));
- assertEquals(
- "1 bottle of beer on the wall. "
- + "1 bottle of beer. "
- + "Take it down and pass it around. "
- + "No bottles of beer" + " on the wall." + NL
- //
- + "No bottles of beer" + " on the wall. "
- + "No bottles of beer. "
- + "There are no more to pass around. "
- + "No bottles of beer" + " on the wall." + NL,
- actualOutput);
- }
- public void testTwoBottleOfBeerOnTheWall() {
- final String actualOutput = captureStandardOutput(() -> NumberOfBottlesOfBeerOnTheWall.singNumberOfBottlesOfBeerOnTheWall(2));
- assertEquals(
- "2 bottles of beer" + " on the wall. "
- + "2 bottles of beer. "
- + "Take one down and pass it around. "
- + "1 bottle of beer on the wall." + NL
- //
- + "1 bottle of beer on the wall. "
- + "1 bottle of beer. "
- + "Take it down and pass it around. "
- + "No bottles of beer" + " on the wall." + NL
- //
- + "No bottles of beer" + " on the wall. "
- + "No bottles of beer. "
- + "There are no more to pass around. "
- + "No bottles of beer" + " on the wall." + NL,
- actualOutput);
- }
- public void testThreeBottleOfBeerOnTheWall() {
- final String actualOutput = captureStandardOutput(() -> NumberOfBottlesOfBeerOnTheWall.singNumberOfBottlesOfBeerOnTheWall(3));
- assertEquals(
- "3 bottles of beer on the wall. "
- + "3 bottles of beer. "
- + "Take one down and pass it around. "
- + "2 bottles of beer on the wall." + NL
- //
- + "2 bottles of beer" + " on the wall. "
- + "2 bottles of beer. "
- + "Take one down and pass it around. "
- + "1 bottle of beer on the wall." + NL
- //
- + "1 bottle of beer on the wall. "
- + "1 bottle of beer. "
- + "Take it down and pass it around. "
- + "No bottles of beer" + " on the wall." + NL
- //
- + "No bottles of beer" + " on the wall. "
- + "No bottles of beer. "
- + "There are no more to pass around. "
- + "No bottles of beer" + " on the wall." + NL,
- actualOutput);
- }
- private String captureStandardOutput(final Runnable callback) {
- final PrintStream oldSystemOut = System.out;
- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- final PrintStream printStream = new PrintStream(byteArrayOutputStream);
- System.setOut(printStream);
- try {
- callback.run();
- } finally {
- System.setOut(oldSystemOut);
- printStream.close();
- }
- return byteArrayOutputStream.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement