|
| 1 | +import java.io.ByteArrayOutputStream; |
| 2 | +import java.io.PrintStream; |
| 3 | + |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.After; |
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | +import static org.junit.Assert.assertThat; |
| 9 | +import static org.hamcrest.CoreMatchers.instanceOf; |
| 10 | +import static org.hamcrest.CoreMatchers.containsString; |
| 11 | + |
| 12 | +public class LETest { |
| 13 | + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 14 | + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); |
| 15 | + private final PrintStream originalOut = System.out; |
| 16 | + private final PrintStream originalErr = System.err; |
| 17 | + |
| 18 | + @Before |
| 19 | + public void setUpStreams() { |
| 20 | + System.setOut(new PrintStream(outContent)); |
| 21 | + System.setErr(new PrintStream(errContent)); |
| 22 | + } |
| 23 | + |
| 24 | + @After |
| 25 | + public void restoreStreams() { |
| 26 | + System.setOut(originalOut); |
| 27 | + System.setErr(originalErr); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void checkHello() { |
| 32 | + String expected = "Hello!"; |
| 33 | + SavingsAccount savingsTest = new SavingsAccount(2000); |
| 34 | + |
| 35 | + savingsTest.checkBalance(); |
| 36 | + |
| 37 | + assertThat("Does your `checkBalance()` method print \"Hello!\"?", outContent.toString(), containsString(expected)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void checkYourBalanceIs() { |
| 42 | + String expected = "Your balance is 2000"; |
| 43 | + SavingsAccount savingsTest = new SavingsAccount(2000); |
| 44 | + |
| 45 | + savingsTest.checkBalance(); |
| 46 | + |
| 47 | + assertThat("Does your `checkBalance()` method print \"Your balance is \" + balance?", outContent.toString(), containsString(expected)); |
| 48 | + } |
| 49 | +} |
0 commit comments