AccountServiceDefaultTest.java
3.21 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
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package org.legrog.web.account;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.legrog.entities.*;
import org.mockito.Mock;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Classe testing AccountServiceDefault.
*/
public class AccountServiceDefaultTest {
AccountServiceDefault accountServiceDefault;
@BeforeEach
public void setUp(@Mock AccountRepository accountRepository,
@Mock AccountSearchRepository accountSearchRepository) throws Exception {
accountServiceDefault = Mockito.spy(new AccountServiceDefault(accountRepository, accountSearchRepository));
}
@Nested
@DisplayName("search method")
class SearchTests {
@DisplayName("When called, should delegate search to AccountSearchRepository")
@Test
public void testDelegateSearchToAccountSearchRepository(@Mock AccountSearchRepository accountSearchRepository) throws SearchingException {
accountServiceDefault.search("3");
Mockito.verify(accountSearchRepository).search("3");
}
@DisplayName("When getting IndexedAccounts from search, should convertIndexedAccountsIntoAccounts them")
@Test
public void testConvertReturnedIndexedAccounts(@Mock AccountSearchRepository accountSearchRepository) throws SearchingException {
List<IndexedAccount> indexedAccounts = new ArrayList<>();
when(accountSearchRepository.search("4")).thenReturn(indexedAccounts);
accountServiceDefault.search("4");
verify(accountServiceDefault, times(1)).convertIndexedAccountsIntoAccounts(indexedAccounts);
}
@DisplayName("When called, should return the Accounts it gets from convertIndexedAccountsIntoAccounts")
@Test
public void testReturnFromConvert(@Mock AccountSearchRepository accountSearchRepository) throws SearchingException {
List<Account> accounts = new ArrayList<>();
List<IndexedAccount> indexedAccounts = new ArrayList<>();
Mockito.doReturn(accounts).when(accountServiceDefault).convertIndexedAccountsIntoAccounts(indexedAccounts);
when(accountSearchRepository.search("5")).thenReturn(indexedAccounts);
assertThat(accountServiceDefault.search("5")).isEqualTo(accounts);
}
}
@Nested
@DisplayName("convertIndexedAccountsIntoAccounts method")
class ConvertTests {
@DisplayName("When called, should return the Accounts it gets from findByUserIdIn")
@Test
public void testReturnFromFind(@Mock AccountRepository accountRepository) {
List<Account> accounts = new ArrayList<>();
List<IndexedAccount> indexedAccounts = new ArrayList<>();
when(accountRepository.findByUserIdIn(Mockito.any())).thenReturn(accounts);
assertThat(accountServiceDefault.convertIndexedAccountsIntoAccounts(indexedAccounts)).isEqualTo(accounts);
}
}
}