22
33import org .junit .jupiter .api .BeforeEach ;
44import org .junit .jupiter .params .ParameterizedTest ;
5+ import org .junit .jupiter .params .provider .Arguments ;
56import org .junit .jupiter .params .provider .MethodSource ;
67import org .junit .jupiter .params .provider .ValueSource ;
78
9+ import javax .util .streamex .StreamEx ;
810import java .util .stream .IntStream ;
911import java .util .stream .Stream ;
1012
1113import static org .assertj .core .api .Assertions .assertThat ;
1214
1315public class TieredPricingShould {
16+ private static final IntStream FIRST_TIER_RANGE = IntStream .rangeClosed (1 , 2 );
17+ private static final IntStream SECOND_TIER_RANGE = IntStream .rangeClosed (3 , 10 );
18+ private static final IntStream THIRD_TIER_RANGE = IntStream .rangeClosed (11 , 25 );
19+ private static final IntStream FOURTH_TIER_RANGE = IntStream .rangeClosed (26 , 50 );
1420
1521 private TieredPricing tieredPricing ;
1622
@@ -20,35 +26,11 @@ void setUp() {
2026 }
2127
2228 @ ParameterizedTest
23- @ MethodSource ("rangeFirstTier " )
24- void returnTotalPriceForFirstTier (int subscriptions ) {
29+ @ MethodSource ("tierRanges " )
30+ void returnTotalPriceForEachTier (int subscriptions , double unitPrice ) {
2531 double totalPrice = tieredPricing .totalPrice (subscriptions );
2632
27- assertThat (totalPrice ).isEqualTo (subscriptions * 299.0 );
28- }
29-
30- @ ParameterizedTest
31- @ MethodSource ("rangeSecondTier" )
32- void returnTotalPriceForSecondTier (int subscriptions ) {
33- double totalPrice = tieredPricing .totalPrice (subscriptions );
34-
35- assertThat (totalPrice ).isEqualTo (subscriptions * 239.0 );
36- }
37-
38- @ ParameterizedTest
39- @ MethodSource ("rangeThirdTier" )
40- void returnTotalPriceForThirdTier (int subscriptions ) {
41- double totalPrice = tieredPricing .totalPrice (subscriptions );
42-
43- assertThat (totalPrice ).isEqualTo (subscriptions * 219.0 );
44- }
45-
46- @ ParameterizedTest
47- @ MethodSource ("rangeFourthTier" )
48- void returnTotalPriceForFourthTier (int subscriptions ) {
49- double totalPrice = tieredPricing .totalPrice (subscriptions );
50-
51- assertThat (totalPrice ).isEqualTo (subscriptions * 199.0 );
33+ assertThat (totalPrice ).isEqualTo (subscriptions * unitPrice );
5234 }
5335
5436 @ ParameterizedTest
@@ -59,19 +41,14 @@ void returnTotalPriceForLastTier(int subscriptions) {
5941 assertThat (totalPrice ).isEqualTo (subscriptions * 149.0 );
6042 }
6143
62- private static Stream <Integer > rangeFirstTier () {
63- return IntStream .rangeClosed (1 , 2 ).boxed ();
64- }
65-
66- private static Stream <Integer > rangeSecondTier () {
67- return IntStream .rangeClosed (3 , 10 ).boxed ();
68- }
69-
70- private static Stream <Integer > rangeThirdTier () {
71- return IntStream .rangeClosed (11 , 25 ).boxed ();
72- }
73-
74- private static Stream <Integer > rangeFourthTier () {
75- return IntStream .rangeClosed (26 , 50 ).boxed ();
44+ private static Stream <Arguments > tierRanges () {
45+ Stream <Arguments > firstTier = FIRST_TIER_RANGE .mapToObj (i -> Arguments .of (i , 299.0 ));
46+ Stream <Arguments > secondTier = SECOND_TIER_RANGE .mapToObj (i -> Arguments .of (i , 239.0 ));
47+ Stream <Arguments > thirdTier = THIRD_TIER_RANGE .mapToObj (i -> Arguments .of (i , 219.0 ));
48+ Stream <Arguments > fourthTier = FOURTH_TIER_RANGE .mapToObj (i -> Arguments .of (i , 199.0 ));
49+ return StreamEx .of (firstTier )
50+ .append (secondTier )
51+ .append (thirdTier )
52+ .append (fourthTier );
7653 }
7754}
0 commit comments