View Javadoc
1   /*
2    *    Copyright 2010-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * @author Eduardo Macarron
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      // given
46      Account account = new Account();
47  
48      // when
49      accountService.insertAccount(account);
50  
51      // then
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      // given
60      Account account = new Account();
61      account.setPassword("foo");
62  
63      // when
64      accountService.updateAccount(account);
65  
66      // then
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      // given
75      String username = "bar";
76      Account expectedAccount = new Account();
77      when(accountMapper.getAccountByUsername(username)).thenReturn(expectedAccount);
78  
79      // when
80      Account account = accountService.getAccount(username);
81  
82      // then
83      assertThat(account).isSameAs(expectedAccount);
84    }
85  
86    @Test
87    void shouldCallTheMapperToGetAccountAnUsernameAndPassword() {
88      // given
89      String username = "bar";
90      String password = "foo";
91  
92      // when
93      Account expectedAccount = new Account();
94      when(accountMapper.getAccountByUsernameAndPassword(username, password)).thenReturn(expectedAccount);
95      Account account = accountService.getAccount(username, password);
96  
97      // then
98      assertThat(account).isSameAs(expectedAccount);
99    }
100 
101 }