+
+ @Test
+ @DisplayName("A sum can be ended, if it does exist")
+ public void testEndSumPossibleIfSumExists()
+ {
+ AdderBusinessLogic adder = new AdderBusinessLogic();
+ adder.startSum("foo");
+ assertThatNoException().isThrownBy(() -> adder.endSum("foo"));
+ }
+
+ @Test
+ @DisplayName("An existing Sum returns a non-null value, if ended")
+ public void testEndSumReturnsNonNullValueIfSumExists()
+ {
+ AdderBusinessLogic adder = new AdderBusinessLogic();
+ adder.startSum("foo");
+ assertThat(adder.endSum("foo")).isNotNull();
+ }
+
+ @Test
+ @DisplayName("An existing Sum returns a non-negative value, if ended")
+ public void testEndSumReturnsNonNegativeValueIfSumExists()
+ {
+ AdderBusinessLogic adder = new AdderBusinessLogic();
+ adder.startSum("foo");
+ assertThat(adder.endSum("foo")).isNotNegative();
+ }
+
+ @Test
+ @DisplayName("Ending a non-existing sum, causes an IllegalStateException")
+ public void testEndSumCausesExceptionIfNotExists()
+ {
+ AdderBusinessLogic adder = new AdderBusinessLogic();
+ assertThatIllegalStateException().isThrownBy(() -> adder.endSum("foo"));
+ }
+
+ @Test
+ @DisplayName("Adding to a non-existent sum causes an IllegalStateException")
+ public void testAddToSumCausesExceptionIfNotExists()
+ {
+ AdderBusinessLogic adder = new AdderBusinessLogic();
+ assertThatIllegalStateException().isThrownBy(() -> adder.addToSum("foo", 1));
+ }
+
+ @Test
+ @DisplayName("Adding a null-value to an existing sum causes an IllegalArgumentException")
+ public void testAddSumWithNullValueToExistingSumCausesException()
+ {
+ AdderBusinessLogic adder = new AdderBusinessLogic();
+ adder.startSum("foo");
+ assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", null));
+ }
+
+ @ParameterizedTest(name = "{index}: Adding {0}")
+ @DisplayName("Adding a non-positive value to an existing sum causes an IllegalArgumentException")
+ @ValueSource(ints = { 0, -1, -6, -66, Integer.MIN_VALUE })
+ public void testAddSumWithNonPositiveValueToExistingSumCausesException(int value)
+ {
+ AdderBusinessLogic adder = new AdderBusinessLogic();
+ adder.startSum("foo");
+ assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", value));
+ }
+
+ @Test
+ @DisplayName("Can add a positive value to an existing sum")
+ public void testAddSumWithPositiveValuePossibleIfSumExists()
+ {
+ AdderBusinessLogic adder = new AdderBusinessLogic();
+ adder.startSum("foo");
+ assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", -1));
+ }
+
+ @ParameterizedTest(name = "{index}: Summing up {0}")
+ @DisplayName("Adds up numbers correctly")
+ @MethodSource("numbersProvider")
+ public void testAddSumAddsUpNumbersCorrectlyIfSumExists(int... numbers)
+ {
+ long expectedResult = Arrays.stream(numbers).sum();
+ AdderBusinessLogic adder = new AdderBusinessLogic();
+ adder.startSum("foo");
+ Arrays.stream(numbers).forEach(number -> adder.addToSum("foo", number));
+ assertThat(adder.endSum("foo")).isEqualTo(expectedResult);
+ }
+
+ static Stream<Arguments> numbersProvider() {
+ return Stream.of(
+ Arguments.of((Object) IntStream.rangeClosed(1,9).toArray()),
+ Arguments.of((Object) IntStream.rangeClosed(1,19).toArray()),
+ Arguments.of((Object) IntStream.rangeClosed(1,66).toArray()));
+ }