1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.jpetstore.service;
17
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.mockito.ArgumentMatchers.eq;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.mybatis.jpetstore.domain.Account;
29 import org.mybatis.jpetstore.mapper.AccountMapper;
30
31
32
33
34 @ExtendWith(MockitoExtension.class)
35 class AccountServiceTest {
36
37 @Mock
38 private AccountMapper accountMapper;
39
40 @InjectMocks
41 private AccountService accountService;
42
43 @Test
44 void shouldCallTheMapperToInsertAnAccount() {
45
46 Account account = new Account();
47
48
49 accountService.insertAccount(account);
50
51
52 verify(accountMapper).insertAccount(eq(account));
53 verify(accountMapper).insertProfile(eq(account));
54 verify(accountMapper).insertSignon(eq(account));
55 }
56
57 @Test
58 void shouldCallTheMapperToUpdateAnAccount() {
59
60 Account account = new Account();
61 account.setPassword("foo");
62
63
64 accountService.updateAccount(account);
65
66
67 verify(accountMapper).updateAccount(eq(account));
68 verify(accountMapper).updateProfile(eq(account));
69 verify(accountMapper).updateSignon(eq(account));
70 }
71
72 @Test
73 void shouldCallTheMapperToGetAccountAnUsername() {
74
75 String username = "bar";
76 Account expectedAccount = new Account();
77 when(accountMapper.getAccountByUsername(username)).thenReturn(expectedAccount);
78
79
80 Account account = accountService.getAccount(username);
81
82
83 assertThat(account).isSameAs(expectedAccount);
84 }
85
86 @Test
87 void shouldCallTheMapperToGetAccountAnUsernameAndPassword() {
88
89 String username = "bar";
90 String password = "foo";
91
92
93 Account expectedAccount = new Account();
94 when(accountMapper.getAccountByUsernameAndPassword(username, password)).thenReturn(expectedAccount);
95 Account account = accountService.getAccount(username, password);
96
97
98 assertThat(account).isSameAs(expectedAccount);
99 }
100
101 }