-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntranceMachineTest.java
53 lines (43 loc) · 2.28 KB
/
EntranceMachineTest.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package y2023.m12.d14.fsm.swch;
import cn.hutool.core.lang.Assert;
import org.junit.Test;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
public class EntranceMachineTest {
@Test
public void should_be_unlocked_when_insert_coin_given_a_entrance_machine_with_locked_state() {
EntranceMachine entranceMachine = new EntranceMachine(EntranceMachineState.LOCKED);
String result = entranceMachine.execute(Action.INSERT_COIN);
Assert.equals(result, "opened");
Assert.equals(entranceMachine.getState(), EntranceMachineState.UNLOCKED);
}
@Test
public void should_be_locked_and_alarm_when_pass_given_a_entrance_machine_with_locked_state() {
EntranceMachine entranceMachine = new EntranceMachine(EntranceMachineState.LOCKED);
String result = entranceMachine.execute(Action.PASS);
Assert.equals(result, "alarm");
Assert.equals(entranceMachine.getState(), EntranceMachineState.LOCKED);
}
@Test
public void should_fail_when_execute_invalid_action_given_a_entrance_with_locked_state() {
EntranceMachine entranceMachine = new EntranceMachine(EntranceMachineState.LOCKED);
InvalidActionException invalidActionException = assertThrows(InvalidActionException.class, () -> entranceMachine.execute(null));
String expectedMessage = "action can not be null";
String actualMessage = invalidActionException.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
@Test
public void should_locked_when_pass_given_a_entrance_machine_with_unlocked_state() {
EntranceMachine entranceMachine = new EntranceMachine(EntranceMachineState.UNLOCKED);
String result = entranceMachine.execute(Action.PASS);
Assert.equals(result, "closed");
Assert.equals(entranceMachine.getState(), EntranceMachineState.LOCKED);
}
@Test
public void should_refund_and_unlocked_when_insert_coin_given_a_entrance_machine_with_unlocked_state() {
EntranceMachine entranceMachine = new EntranceMachine(EntranceMachineState.UNLOCKED);
String result = entranceMachine.execute(Action.INSERT_COIN);
Assert.equals(result, "refund");
Assert.equals(entranceMachine.getState(), EntranceMachineState.UNLOCKED);
}
}