|
1 | 1 | package com.example.paul.unit;
|
2 | 2 |
|
3 | 3 | import com.example.paul.models.Account;
|
| 4 | +import com.example.paul.models.Transaction; |
4 | 5 | import com.example.paul.repositories.AccountRepository;
|
5 | 6 | import com.example.paul.repositories.TransactionRepository;
|
6 | 7 | import com.example.paul.services.AccountService;
|
7 | 8 | import org.junit.jupiter.api.BeforeEach;
|
8 | 9 | import org.junit.jupiter.api.Test;
|
9 | 10 | import org.junit.jupiter.api.extension.ExtendWith;
|
10 |
| -import org.springframework.beans.factory.annotation.Autowired; |
11 |
| -import org.springframework.boot.test.context.TestConfiguration; |
12 |
| -import org.springframework.boot.test.mock.mockito.MockBean; |
13 |
| -import org.springframework.context.annotation.Bean; |
14 |
| -import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.junit.jupiter.MockitoExtension; |
15 | 13 |
|
| 14 | +import java.util.List; |
16 | 15 | import java.util.Optional;
|
17 | 16 |
|
18 | 17 | import static org.assertj.core.api.Assertions.assertThat;
|
19 | 18 | import static org.mockito.Mockito.when;
|
20 | 19 |
|
21 |
| -@ExtendWith(SpringExtension.class) |
| 20 | +@ExtendWith(MockitoExtension.class) |
22 | 21 | class AccountServiceTest {
|
23 | 22 |
|
24 |
| - @TestConfiguration |
25 |
| - static class AccountServiceTestContextConfiguration { |
26 |
| - |
27 |
| - @Bean |
28 |
| - public AccountService accountService() { |
29 |
| - return new AccountService(); |
30 |
| - } |
31 |
| - } |
32 |
| - |
33 |
| - @Autowired |
34 |
| - private AccountService accountService; |
35 |
| - |
36 |
| - @MockBean |
| 23 | + @Mock |
37 | 24 | private AccountRepository accountRepository;
|
38 |
| - |
39 |
| - @MockBean |
| 25 | + @Mock |
40 | 26 | private TransactionRepository transactionRepository;
|
41 | 27 |
|
| 28 | + public AccountService underTest; |
| 29 | + |
42 | 30 | @BeforeEach
|
43 | 31 | void setUp() {
|
| 32 | + underTest = new AccountService(accountRepository, transactionRepository); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void shouldReturnAccountBySortCodeAndAccountNumberWhenPresent() { |
44 | 37 | var account = new Account(1L, "53-68-92", "78901234", 10.1, "Some Bank", "John");
|
| 38 | + when(accountRepository.findBySortCodeAndAccountNumber("53-68-92", "78901234")) |
| 39 | + .thenReturn(Optional.of(account)); |
| 40 | + |
| 41 | + var result = underTest.getAccount("53-68-92", "78901234"); |
45 | 42 |
|
| 43 | + assertThat(result.getOwnerName()).isEqualTo(account.getOwnerName()); |
| 44 | + assertThat(result.getSortCode()).isEqualTo(account.getSortCode()); |
| 45 | + assertThat(result.getAccountNumber()).isEqualTo(account.getAccountNumber()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void shouldReturnTransactionsForAccount() { |
| 50 | + var account = new Account(1L, "53-68-92", "78901234", 10.1, "Some Bank", "John"); |
46 | 51 | when(accountRepository.findBySortCodeAndAccountNumber("53-68-92", "78901234"))
|
47 | 52 | .thenReturn(Optional.of(account));
|
| 53 | + var transaction1 = new Transaction(); |
| 54 | + var transaction2 = new Transaction(); |
| 55 | + transaction1.setReference("a"); |
| 56 | + transaction2.setReference("b"); |
| 57 | + when(transactionRepository.findBySourceAccountIdOrderByInitiationDate(account.getId())) |
| 58 | + .thenReturn(List.of(transaction1, transaction2)); |
| 59 | + |
| 60 | + var result = underTest.getAccount("53-68-92", "78901234"); |
| 61 | + |
| 62 | + assertThat(result.getTransactions()).hasSize(2); |
| 63 | + assertThat(result.getTransactions()).extracting("reference").containsExactly("a", "b"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void shouldReturnNullWhenAccountBySortCodeAndAccountNotFound() { |
| 68 | + when(accountRepository.findBySortCodeAndAccountNumber("53-68-92", "78901234")) |
| 69 | + .thenReturn(Optional.empty()); |
| 70 | + |
| 71 | + var result = underTest.getAccount("53-68-92", "78901234"); |
| 72 | + |
| 73 | + assertThat(result).isNull(); |
48 | 74 | }
|
49 | 75 |
|
50 | 76 | @Test
|
51 |
| - void whenAccountDetails_thenAccountShouldBeFound() { |
52 |
| - var account = accountService.getAccount("53-68-92", "78901234"); |
| 77 | + void shouldReturnAccountByAccountNumberWhenPresent() { |
| 78 | + } |
53 | 79 |
|
54 |
| - assertThat(account.getOwnerName()).isEqualTo("John"); |
55 |
| - assertThat(account.getSortCode()).isEqualTo("53-68-92"); |
56 |
| - assertThat(account.getAccountNumber()).isEqualTo("78901234"); |
| 80 | + @Test |
| 81 | + void shouldReturnNullWhenAccountByAccountNotFound() { |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void shouldCreateAccount() { |
57 | 86 | }
|
58 | 87 | }
|
0 commit comments