|
| 1 | +package com.craftsmanshipinsoftware.retirement; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 5 | + |
| 6 | +import java.io.ByteArrayInputStream; |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.io.PrintStream; |
| 9 | +import java.time.Year; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.params.ParameterizedTest; |
| 12 | +import org.junit.jupiter.params.provider.ValueSource; |
| 13 | + |
| 14 | +class RetirementCalculatorTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + void asksForCurrentAge() { |
| 18 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 19 | + RetirementCalculator calculator = new RetirementCalculator(new ByteArrayInputStream("25\n65".getBytes()), |
| 20 | + new PrintStream(outputStream), Year::now); |
| 21 | + |
| 22 | + calculator.printYearsLeft(); |
| 23 | + |
| 24 | + assertThat(outputStream.toString()).contains("What is your current age?"); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + void asksForRetirementAge() { |
| 29 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 30 | + RetirementCalculator calculator = new RetirementCalculator(new ByteArrayInputStream("25\n65".getBytes()), |
| 31 | + new PrintStream(outputStream), Year::now); |
| 32 | + |
| 33 | + calculator.printYearsLeft(); |
| 34 | + |
| 35 | + assertThat(outputStream.toString()).contains("At what age would you like to retire?"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void displaysYearsLeftUntilRetirement() { |
| 40 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 41 | + RetirementCalculator calculator = new RetirementCalculator(new ByteArrayInputStream("25\n65".getBytes()), |
| 42 | + new PrintStream(outputStream), new FakeYearProvider()); |
| 43 | + |
| 44 | + calculator.printYearsLeft(); |
| 45 | + |
| 46 | + assertThat(outputStream.toString()).containsSequence("You have 40 years left until you can retire.\n", |
| 47 | + "It's 2015, so you can retire in 2055.\n"); |
| 48 | + } |
| 49 | + |
| 50 | + @ParameterizedTest |
| 51 | + @ValueSource(strings = {"65\n65", "75\n65"}) |
| 52 | + void displaysYouCanAlreadyRetireWhenCurrentAgeIsRetirementAgeOrMore(String input) { |
| 53 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 54 | + RetirementCalculator calculator = new RetirementCalculator(new ByteArrayInputStream(input.getBytes()), |
| 55 | + new PrintStream(outputStream), Year::now); |
| 56 | + |
| 57 | + calculator.printYearsLeft(); |
| 58 | + |
| 59 | + assertThat(outputStream.toString()).contains("You can already retire."); |
| 60 | + } |
| 61 | + |
| 62 | + @ParameterizedTest |
| 63 | + @ValueSource(strings = {"\n65", "25\n"}) |
| 64 | + void inputIsRequired(String input) { |
| 65 | + RetirementCalculator calculator = new RetirementCalculator(new ByteArrayInputStream(input.getBytes()), |
| 66 | + new PrintStream(new ByteArrayOutputStream()), Year::now); |
| 67 | + |
| 68 | + assertThatIllegalArgumentException() |
| 69 | + .isThrownBy(calculator::printYearsLeft) |
| 70 | + .withMessage("Input must not be empty!"); |
| 71 | + } |
| 72 | + |
| 73 | + @ParameterizedTest |
| 74 | + @ValueSource(strings = {"abc\n65", "25\nabc"}) |
| 75 | + void inputMustBeANumber(String input) { |
| 76 | + RetirementCalculator calculator = new RetirementCalculator(new ByteArrayInputStream(input.getBytes()), |
| 77 | + new PrintStream(new ByteArrayOutputStream()), Year::now); |
| 78 | + |
| 79 | + assertThatIllegalArgumentException() |
| 80 | + .isThrownBy(calculator::printYearsLeft) |
| 81 | + .withMessage("Please enter a valid number! Input: abc"); |
| 82 | + } |
| 83 | + |
| 84 | + @ParameterizedTest |
| 85 | + @ValueSource(strings = {"-1\n65", "25\n-1"}) |
| 86 | + void inputMustBeAPositiveNumber(String input) { |
| 87 | + RetirementCalculator calculator = new RetirementCalculator(new ByteArrayInputStream(input.getBytes()), |
| 88 | + new PrintStream(new ByteArrayOutputStream()), Year::now); |
| 89 | + |
| 90 | + assertThatIllegalArgumentException() |
| 91 | + .isThrownBy(calculator::printYearsLeft) |
| 92 | + .withMessage("Please enter a positive number! Input: -1"); |
| 93 | + } |
| 94 | + |
| 95 | + static final class FakeYearProvider implements YearProvider { |
| 96 | + |
| 97 | + @Override |
| 98 | + public Year currentYear() { |
| 99 | + return Year.of(2015); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments