1+ package pl .mperor .lab .java .design .pattern .behavioral .strategy .classic ;
2+
3+
4+ import org .junit .jupiter .api .Assertions ;
5+ import org .junit .jupiter .api .Test ;
6+ import pl .mperor .lab .common .TestUtils ;
7+
8+ import java .time .LocalDate ;
9+
10+ public class PaymentStrategyTest {
11+
12+ @ Test
13+ public void shouldShoppingAllowToPayWithDifferentPaymentStrategy () {
14+ Shopping shopping = new Shopping (new PaypalStrategy ("mypaypall@example.com" , "1234" ));
15+ ShoppingCart shoppingCart = new ShoppingCart ();
16+ shoppingCart .addItem (new Item ("b1" , "Smart TV" , 999_00 ));
17+ shoppingCart .addItem (new Item ("b2" , "Raspberry Pi" , 99_99 ));
18+ shoppingCart .addItem (new Item ("b3" , "DVD Player" , 44_95 ));
19+
20+ var out = TestUtils .setTempSystemOut ();
21+ shopping .pay (shoppingCart );
22+ shopping .setStrategy (new CreditCardStrategy ("John Doe" , "1234 1111 2222 1010" , "123" , LocalDate .parse ("2025-12-31" )));
23+ shopping .pay (shoppingCart );
24+ shopping .pay (shoppingCart , _ -> System .out .println ("Fake payment 💰, do not use in production!" ));
25+
26+ var outLines = out .lines ();
27+ Assertions .assertTrue (outLines .getFirst ().endsWith ("paid with 📱 paypal!" ));
28+ Assertions .assertTrue (outLines .getSecond ().endsWith ("paid by 💳 credit card!" ));
29+ Assertions .assertEquals ("Fake payment 💰, do not use in production!" , outLines .getThird ());
30+ TestUtils .resetSystemOut ();
31+ }
32+
33+ @ Test
34+ public void shouldReturnCorrectTotalPriceWhenItemsAdded () {
35+ var cart = new ShoppingCart ();
36+ cart .addItem (new Item ("A1" , "iPhone X" , 2000 * 100 ));
37+ cart .addItem (new Item ("A2" , "Laptop" , 2500 * 100 ), 2 );
38+ cart .addItem (new Item ("A3" , "Tablet Neo" , 199 * 100 ), 3 );
39+ Assertions .assertEquals (7597d , cart .computeTotal ());
40+ }
41+ }
0 commit comments