|
8 | 8 | public class DecoratorPatternTest { |
9 | 9 |
|
10 | 10 | @Test |
11 | | - void givenACalculator_whenUsingDecoratorWrappingToAddLogging_thenDecoratorWrappingCanBeUsed() { |
| 11 | + void givenACalculator_whenUsingMeteredDecorator_thenMethodCallsAreCountedCorrectly() { |
| 12 | + // ARRANGE |
12 | 13 | Calculator simpleCalc = new SimpleCalculator(); |
13 | | - Calculator decoratedCalc = new LoggingCalculatorDecorator(simpleCalc); |
14 | | - assertEquals(15, decoratedCalc.add(10, 5)); |
15 | | - assertEquals(5, decoratedCalc.subtract(10, 5)); |
| 14 | + |
| 15 | + // Use the MeteredCalculator decorator |
| 16 | + MeteredCalculator decoratedCalc = new MeteredCalculator(simpleCalc); |
| 17 | + |
| 18 | + // ACT |
| 19 | + // Call add twice |
| 20 | + decoratedCalc.add(10, 5); |
| 21 | + decoratedCalc.add(2, 3); |
| 22 | + |
| 23 | + // Call subtract once |
| 24 | + decoratedCalc.subtract(10, 5); |
| 25 | + |
| 26 | + // ASSERT Core Functionality (optional, but good practice) |
| 27 | + assertEquals(15, decoratedCalc.add(10, 5), "Core functionality must still work."); |
| 28 | + |
| 29 | + // ASSERT the call counts |
| 30 | + // 1. Assert 'add' was called 3 times (2 from ACT + 1 from ASSERT Core) |
| 31 | + assertEquals(3, decoratedCalc.getCallCount("add"), "The 'add' method should have been called 3 times."); |
| 32 | + |
| 33 | + // 2. Assert 'subtract' was called 1 time |
| 34 | + assertEquals(1, decoratedCalc.getCallCount("subtract"), "The 'subtract' method should have been called 1 time."); |
16 | 35 | } |
17 | 36 | } |
0 commit comments