1
2
3
4
5
6 package jmockit.loginExample.domain.userLogin;
7
8 import static org.testng.Assert.assertFalse;
9 import static org.testng.Assert.assertThrows;
10 import static org.testng.Assert.assertTrue;
11
12 import mockit.Mock;
13 import mockit.MockUp;
14 import mockit.Tested;
15
16 import org.testng.annotations.BeforeMethod;
17 import org.testng.annotations.Test;
18
19 import jmockit.loginExample.domain.userAccount.UserAccount;
20
21
22
23
24 public final class LoginServiceIntegrationTest {
25
26
27 @Tested
28 LoginService service;
29
30
31 String userId;
32
33
34 String userPassword;
35
36
37 UserAccount account;
38
39
40
41
42 @BeforeMethod
43 public void setUpOneAccountToBeFound() {
44 userId = "john";
45 userPassword = "password";
46 account = new UserAccount(userId, userPassword);
47
48 new MockUp<UserAccount>() {
49 @Mock
50 UserAccount find(String accountId) {
51 return account;
52 }
53 };
54 }
55
56
57
58
59
60
61
62 @Test
63 public void setAccountToLoggedInWhenPasswordMatches() throws Exception {
64
65 service.login(userId, "wrong password");
66 service.login(userId, "wrong password");
67 service.login(userId, userPassword);
68 service.login(userId, "wrong password");
69
70 assertTrue(account.isLoggedIn());
71 assertFalse(account.isRevoked());
72 }
73
74
75
76
77
78
79
80 @Test
81 public void setAccountToRevokedAfterThreeFailedLoginAttempts() throws Exception {
82 service.login(userId, "wrong password");
83 service.login(userId, "wrong password");
84 service.login(userId, "wrong password");
85
86 assertFalse(account.isLoggedIn());
87 assertTrue(account.isRevoked());
88 }
89
90
91
92
93
94
95
96 @Test
97 public void notRevokeSecondAccountAfterTwoFailedAttemptsOnFirstAccount() throws Exception {
98 UserAccount secondAccount = new UserAccount("roger", "password");
99 String accountId = account.getId();
100 account = secondAccount;
101
102 service.login(accountId, "wrong password");
103 service.login(accountId, "wrong password");
104 service.login(secondAccount.getId(), "wrong password");
105
106 assertFalse(secondAccount.isRevoked());
107 }
108
109
110
111
112 @Test
113 public void disallowConcurrentLogins() {
114 account.setLoggedIn(true);
115
116 assertThrows(AccountLoginLimitReachedException.class, () -> {
117 service.login(userId, userPassword);
118 });
119 }
120
121
122
123
124 @Test
125 public void throwExceptionIfAccountNotFound() {
126 account = null;
127
128 assertThrows(UserAccountNotFoundException.class, () -> {
129 service.login("roger", "password");
130 });
131 }
132
133
134
135
136 @Test
137 public void disallowLoggingIntoRevokedAccount() {
138 account.setRevoked(true);
139
140 assertThrows(UserAccountRevokedException.class, () -> {
141 service.login(userId, userPassword);
142 });
143 }
144 }