-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternTest.java
More file actions
26 lines (22 loc) · 1.02 KB
/
Copy pathPatternTest.java
File metadata and controls
26 lines (22 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.hanserwei.patterns.strategy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
/** 验证本模式的协作契约与边界行为. */
public final class PatternTest {
/** 同一业务上下文可以装配不同算法. */
@Test
void switchesAlgorithms() {
assertEquals(1000, new QuoteService(new FlatFee()).quote(600));
assertEquals(1200, new QuoteService(new PerWordFee(2)).quote(600));
assertEquals(0, new QuoteService(new PerWordFee(2)).quote(0));
}
/** 所有策略遵守非负输入约定,并显式报告溢出. */
@Test
void validatesAlgorithmBoundaries() {
assertThrows(IllegalArgumentException.class, () -> new FlatFee().fee(-1));
assertThrows(IllegalArgumentException.class, () -> new PerWordFee(2).fee(-1));
assertThrows(IllegalArgumentException.class, () -> new PerWordFee(-1));
assertThrows(ArithmeticException.class, () -> new PerWordFee(Long.MAX_VALUE).fee(2));
}
}