|
| 1 | +package com.baeldung.stablevalues; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.function.Function; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.BeforeEach |
| 12 | + |
| 13 | +class StableValuesUnitTest { |
| 14 | + |
| 15 | + private Set<String> cities; |
| 16 | + |
| 17 | + private String expensiveMethodToDetermineCountry(String city) { |
| 18 | + switch(city) { |
| 19 | + case "Berlin": |
| 20 | + return "Germany"; |
| 21 | + case "London": |
| 22 | + return "England"; |
| 23 | + case "Madrid": |
| 24 | + return "Spain"; |
| 25 | + case "Paris": |
| 26 | + return "France"; |
| 27 | + default: |
| 28 | + throw new RuntimeException("Unsupported city"); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void givenStableFunctionForCityToCountry_whenValidInputsUsed_thenVerifyFunctionResultsAreExpected() { |
| 34 | + Function<String, String> cityToCountry = StableValue.function(cities, city -> expensiveMethodToDetermineCountry(city)); |
| 35 | + |
| 36 | + assertThat(cityToCountry.apply("London")).isEqualTo("England"); |
| 37 | + assertThat(cityToCountry.apply("Madrid")).isEqualTo("Spain"); |
| 38 | + assertThat(cityToCountry.apply("Paris")).isEqualTo("France"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + void givenStableFunctionForCityToCountry_whenInvalidInputUsed_thenExceptionThrown() { |
| 43 | + Function<String, String> cityToCountry = StableValue.function(cities, city -> expensiveMethodToDetermineCountry(city)); |
| 44 | + |
| 45 | + assertThatIllegalArgumentException().isThrownBy(() -> cityToCountry.apply("Berlin")); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void givenStableListForFiveTimesTable_thenVerifyElementsAreExpected() { |
| 50 | + List<Integer> fiveTimesTable = StableValue.list(11, index -> index * 5); |
| 51 | + |
| 52 | + assertThat(fiveTimesTable.get(0)).isEqualTo(0); |
| 53 | + assertThat(fiveTimesTable.get(1)).isEqualTo(5); |
| 54 | + assertThat(fiveTimesTable.get(2)).isEqualTo(10); |
| 55 | + assertThat(fiveTimesTable.get(3)).isEqualTo(15); |
| 56 | + assertThat(fiveTimesTable.get(4)).isEqualTo(20); |
| 57 | + assertThat(fiveTimesTable.get(5)).isEqualTo(25); |
| 58 | + assertThat(fiveTimesTable.get(6)).isEqualTo(30); |
| 59 | + assertThat(fiveTimesTable.get(7)).isEqualTo(35); |
| 60 | + assertThat(fiveTimesTable.get(8)).isEqualTo(40); |
| 61 | + assertThat(fiveTimesTable.get(9)).isEqualTo(45); |
| 62 | + assertThat(fiveTimesTable.get(10)).isEqualTo(50); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void givenStableMapForCityToCountry_thenVerifyValuesAreExpected() { |
| 67 | + Map<String, String> cityToCountry = StableValue.map(cities, city -> expensiveMethodToDetermineCountry(city)); |
| 68 | + |
| 69 | + assertThat(cityToCountry.get("London")).isEqualTo("England"); |
| 70 | + assertThat(cityToCountry.get("Madrid")).isEqualTo("Spain"); |
| 71 | + assertThat(cityToCountry.get("Paris")).isEqualTo("France"); |
| 72 | + } |
| 73 | + |
| 74 | + @BeforeEach |
| 75 | + void init() { |
| 76 | + cities = Set.of("London", "Madrid", "Paris"); |
| 77 | + } |
| 78 | +} |
0 commit comments