Skip to content

Commit d339eaf

Browse files
Merge pull request #66 from Noor53/feature/junit-tests-for-services
Feature/junit tests for services
2 parents 716a16c + 4ba981a commit d339eaf

File tree

3 files changed

+322
-0
lines changed

3 files changed

+322
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.javatodev.finance.service;
2+
3+
import com.javatodev.finance.exception.EntityNotFoundException;
4+
import com.javatodev.finance.model.dto.BankAccount;
5+
import com.javatodev.finance.model.dto.UtilityAccount;
6+
import com.javatodev.finance.model.entity.BankAccountEntity;
7+
import com.javatodev.finance.model.entity.UtilityAccountEntity;
8+
import com.javatodev.finance.repository.BankAccountRepository;
9+
import com.javatodev.finance.repository.UtilityAccountRepository;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import static org.junit.jupiter.api.Assertions.*;
13+
import static org.mockito.Mockito.*;
14+
15+
class AccountServiceTest {
16+
17+
private BankAccountRepository bankAccountRepository;
18+
private UtilityAccountRepository utilityAccountRepository;
19+
private AccountService accountService;
20+
21+
@BeforeEach
22+
void setUp() {
23+
bankAccountRepository = mock(BankAccountRepository.class);
24+
utilityAccountRepository = mock(UtilityAccountRepository.class);
25+
accountService = new AccountService(bankAccountRepository, utilityAccountRepository);
26+
}
27+
28+
@Test
29+
void readBankAccount_found() {
30+
BankAccountEntity entity = new BankAccountEntity();
31+
entity.setNumber("123");
32+
when(bankAccountRepository.findByNumber("123")).thenReturn(java.util.Optional.of(entity));
33+
34+
BankAccount dto = accountService.readBankAccount("123");
35+
assertNotNull(dto);
36+
}
37+
38+
@Test
39+
void readBankAccount_notFound() {
40+
when(bankAccountRepository.findByNumber("123")).thenReturn(java.util.Optional.empty());
41+
assertThrows(EntityNotFoundException.class, () -> accountService.readBankAccount("123"));
42+
}
43+
44+
@Test
45+
void readUtilityAccount_byProvider_found() {
46+
UtilityAccountEntity entity = new UtilityAccountEntity();
47+
entity.setProviderName("ProviderA");
48+
when(utilityAccountRepository.findByProviderName("ProviderA")).thenReturn(java.util.Optional.of(entity));
49+
50+
UtilityAccount dto = accountService.readUtilityAccount("ProviderA");
51+
assertNotNull(dto);
52+
}
53+
54+
@Test
55+
void readUtilityAccount_byProvider_notFound() {
56+
when(utilityAccountRepository.findByProviderName("ProviderA")).thenReturn(java.util.Optional.empty());
57+
assertThrows(EntityNotFoundException.class, () -> accountService.readUtilityAccount("ProviderA"));
58+
}
59+
60+
@Test
61+
void readUtilityAccount_byId_found() {
62+
UtilityAccountEntity entity = new UtilityAccountEntity();
63+
entity.setId(1L);
64+
when(utilityAccountRepository.findById(1L)).thenReturn(java.util.Optional.of(entity));
65+
66+
UtilityAccount dto = accountService.readUtilityAccount(1L);
67+
assertNotNull(dto);
68+
}
69+
70+
@Test
71+
void readUtilityAccount_byId_notFound() {
72+
when(utilityAccountRepository.findById(1L)).thenReturn(java.util.Optional.empty());
73+
assertThrows(EntityNotFoundException.class, () -> accountService.readUtilityAccount(1L));
74+
}
75+
}
76+
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package com.javatodev.finance.service;
2+
3+
import com.javatodev.finance.exception.EntityNotFoundException;
4+
import com.javatodev.finance.exception.GlobalErrorCode;
5+
import com.javatodev.finance.exception.InsufficientFundsException;
6+
import com.javatodev.finance.model.TransactionType;
7+
import com.javatodev.finance.model.dto.BankAccount;
8+
import com.javatodev.finance.model.dto.UtilityAccount;
9+
import com.javatodev.finance.model.dto.request.FundTransferRequest;
10+
import com.javatodev.finance.model.dto.request.UtilityPaymentRequest;
11+
import com.javatodev.finance.model.dto.response.FundTransferResponse;
12+
import com.javatodev.finance.model.dto.response.UtilityPaymentResponse;
13+
import com.javatodev.finance.model.entity.BankAccountEntity;
14+
import com.javatodev.finance.model.entity.TransactionEntity;
15+
import com.javatodev.finance.repository.BankAccountRepository;
16+
import com.javatodev.finance.repository.TransactionRepository;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Test;
19+
import org.mockito.ArgumentCaptor;
20+
import java.math.BigDecimal;
21+
import java.util.Optional;
22+
import static org.junit.jupiter.api.Assertions.*;
23+
import static org.mockito.Mockito.*;
24+
25+
class TransactionServiceTest {
26+
private AccountService accountService;
27+
private BankAccountRepository bankAccountRepository;
28+
private TransactionRepository transactionRepository;
29+
private TransactionService transactionService;
30+
31+
@BeforeEach
32+
void setUp() {
33+
accountService = mock(AccountService.class);
34+
bankAccountRepository = mock(BankAccountRepository.class);
35+
transactionRepository = mock(TransactionRepository.class);
36+
transactionService = new TransactionService(accountService, bankAccountRepository, transactionRepository);
37+
}
38+
39+
@Test
40+
void fundTransfer_success() {
41+
FundTransferRequest request = new FundTransferRequest();
42+
request.setFromAccount("A1");
43+
request.setToAccount("A2");
44+
request.setAmount(BigDecimal.valueOf(100));
45+
46+
BankAccount from = new BankAccount();
47+
from.setNumber("A1");
48+
from.setActualBalance(BigDecimal.valueOf(200));
49+
BankAccount to = new BankAccount();
50+
to.setNumber("A2");
51+
to.setActualBalance(BigDecimal.valueOf(50));
52+
53+
when(accountService.readBankAccount("A1")).thenReturn(from);
54+
when(accountService.readBankAccount("A2")).thenReturn(to);
55+
BankAccountEntity fromEntity = new BankAccountEntity();
56+
fromEntity.setNumber("A1");
57+
fromEntity.setActualBalance(BigDecimal.valueOf(200));
58+
BankAccountEntity toEntity = new BankAccountEntity();
59+
toEntity.setNumber("A2");
60+
toEntity.setActualBalance(BigDecimal.valueOf(50));
61+
when(bankAccountRepository.findByNumber("A1")).thenReturn(Optional.of(fromEntity));
62+
when(bankAccountRepository.findByNumber("A2")).thenReturn(Optional.of(toEntity));
63+
64+
FundTransferResponse response = transactionService.fundTransfer(request);
65+
assertNotNull(response.getTransactionId());
66+
assertEquals("Transaction successfully completed", response.getMessage());
67+
}
68+
69+
@Test
70+
void fundTransfer_insufficientFunds() {
71+
FundTransferRequest request = new FundTransferRequest();
72+
request.setFromAccount("A1");
73+
request.setToAccount("A2");
74+
request.setAmount(BigDecimal.valueOf(300));
75+
BankAccount from = new BankAccount();
76+
from.setNumber("A1");
77+
from.setActualBalance(BigDecimal.valueOf(200));
78+
BankAccount to = new BankAccount();
79+
to.setNumber("A2");
80+
to.setActualBalance(BigDecimal.valueOf(50));
81+
when(accountService.readBankAccount("A1")).thenReturn(from);
82+
when(accountService.readBankAccount("A2")).thenReturn(to);
83+
assertThrows(InsufficientFundsException.class, () -> transactionService.fundTransfer(request));
84+
}
85+
86+
@Test
87+
void fundTransfer_accountNotFound() {
88+
FundTransferRequest request = new FundTransferRequest();
89+
request.setFromAccount("A1");
90+
request.setToAccount("A2");
91+
request.setAmount(BigDecimal.valueOf(100));
92+
when(accountService.readBankAccount("A1")).thenThrow(EntityNotFoundException.class);
93+
assertThrows(EntityNotFoundException.class, () -> transactionService.fundTransfer(request));
94+
}
95+
96+
@Test
97+
void utilPayment_success() {
98+
UtilityPaymentRequest request = new UtilityPaymentRequest();
99+
request.setAccount("A1");
100+
request.setProviderId(1L);
101+
request.setAmount(BigDecimal.valueOf(50));
102+
request.setReferenceNumber("REF123");
103+
BankAccount from = new BankAccount();
104+
from.setNumber("A1");
105+
from.setActualBalance(BigDecimal.valueOf(100));
106+
UtilityAccount utility = new UtilityAccount();
107+
utility.setId(1L);
108+
when(accountService.readBankAccount("A1")).thenReturn(from);
109+
when(accountService.readUtilityAccount(1L)).thenReturn(utility);
110+
BankAccountEntity fromEntity = new BankAccountEntity();
111+
fromEntity.setNumber("A1");
112+
fromEntity.setActualBalance(BigDecimal.valueOf(100));
113+
fromEntity.setAvailableBalance(BigDecimal.valueOf(100));
114+
when(bankAccountRepository.findByNumber("A1")).thenReturn(Optional.of(fromEntity));
115+
UtilityPaymentResponse response = transactionService.utilPayment(request);
116+
assertNotNull(response.getTransactionId());
117+
assertEquals("Utility payment successfully completed", response.getMessage());
118+
}
119+
120+
@Test
121+
void utilPayment_insufficientFunds() {
122+
UtilityPaymentRequest request = new UtilityPaymentRequest();
123+
request.setAccount("A1");
124+
request.setProviderId(1L);
125+
request.setAmount(BigDecimal.valueOf(150));
126+
BankAccount from = new BankAccount();
127+
from.setNumber("A1");
128+
from.setActualBalance(BigDecimal.valueOf(100));
129+
when(accountService.readBankAccount("A1")).thenReturn(from);
130+
assertThrows(InsufficientFundsException.class, () -> transactionService.utilPayment(request));
131+
}
132+
133+
@Test
134+
void utilPayment_accountNotFound() {
135+
UtilityPaymentRequest request = new UtilityPaymentRequest();
136+
request.setAccount("A1");
137+
request.setProviderId(1L);
138+
request.setAmount(BigDecimal.valueOf(50));
139+
when(accountService.readBankAccount("A1")).thenThrow(EntityNotFoundException.class);
140+
assertThrows(EntityNotFoundException.class, () -> transactionService.utilPayment(request));
141+
}
142+
143+
@Test
144+
void internalFundTransfer_success() {
145+
BankAccount from = new BankAccount();
146+
from.setNumber("A1");
147+
from.setActualBalance(BigDecimal.valueOf(200));
148+
BankAccount to = new BankAccount();
149+
to.setNumber("A2");
150+
to.setActualBalance(BigDecimal.valueOf(50));
151+
BankAccountEntity fromEntity = new BankAccountEntity();
152+
fromEntity.setNumber("A1");
153+
fromEntity.setActualBalance(BigDecimal.valueOf(200));
154+
fromEntity.setAvailableBalance(BigDecimal.valueOf(200));
155+
BankAccountEntity toEntity = new BankAccountEntity();
156+
toEntity.setNumber("A2");
157+
toEntity.setActualBalance(BigDecimal.valueOf(50));
158+
toEntity.setAvailableBalance(BigDecimal.valueOf(50));
159+
when(bankAccountRepository.findByNumber("A1")).thenReturn(Optional.of(fromEntity));
160+
when(bankAccountRepository.findByNumber("A2")).thenReturn(Optional.of(toEntity));
161+
String transactionId = transactionService.internalFundTransfer(from, to, BigDecimal.valueOf(100));
162+
assertNotNull(transactionId);
163+
verify(bankAccountRepository, times(2)).save(any(BankAccountEntity.class));
164+
verify(transactionRepository, times(2)).save(any(TransactionEntity.class));
165+
}
166+
167+
@Test
168+
void internalFundTransfer_entityNotFound() {
169+
BankAccount from = new BankAccount();
170+
from.setNumber("A1");
171+
BankAccount to = new BankAccount();
172+
to.setNumber("A2");
173+
when(bankAccountRepository.findByNumber("A1")).thenReturn(Optional.empty());
174+
assertThrows(EntityNotFoundException.class, () -> transactionService.internalFundTransfer(from, to, BigDecimal.valueOf(100)));
175+
}
176+
177+
@Test
178+
void validateBalance_throwsException() {
179+
BankAccount account = new BankAccount();
180+
account.setNumber("A1");
181+
account.setActualBalance(BigDecimal.valueOf(50));
182+
assertThrows(InsufficientFundsException.class, () -> {
183+
transactionService.fundTransfer(new FundTransferRequest("A1", "A2", BigDecimal.valueOf(100)));
184+
});
185+
}
186+
}
187+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.javatodev.finance.service;
2+
3+
import com.javatodev.finance.exception.EntityNotFoundException;
4+
import com.javatodev.finance.model.dto.User;
5+
import com.javatodev.finance.model.entity.UserEntity;
6+
import com.javatodev.finance.repository.UserRepository;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.mockito.Mockito;
10+
import org.springframework.data.domain.Page;
11+
import org.springframework.data.domain.PageImpl;
12+
import org.springframework.data.domain.Pageable;
13+
14+
import java.util.Collections;
15+
import java.util.Optional;
16+
import java.util.List;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
class UserServiceTest {
22+
private UserRepository userRepository;
23+
private UserService userService;
24+
25+
@BeforeEach
26+
void setUp() {
27+
userRepository = mock(UserRepository.class);
28+
userService = new UserService(userRepository);
29+
}
30+
31+
@Test
32+
void readUser_found() {
33+
UserEntity entity = new UserEntity();
34+
entity.setIdentificationNumber("ID123");
35+
when(userRepository.findByIdentificationNumber("ID123")).thenReturn(Optional.of(entity));
36+
User result = userService.readUser("ID123");
37+
assertNotNull(result);
38+
assertEquals("ID123", result.getIdentificationNumber());
39+
}
40+
41+
@Test
42+
void readUser_notFound() {
43+
when(userRepository.findByIdentificationNumber("ID123")).thenReturn(Optional.empty());
44+
assertThrows(EntityNotFoundException.class, () -> userService.readUser("ID123"));
45+
}
46+
47+
@Test
48+
void readUsers_success() {
49+
UserEntity entity = new UserEntity();
50+
entity.setIdentificationNumber("ID123");
51+
List<UserEntity> entities = Collections.singletonList(entity);
52+
Page<UserEntity> page = new PageImpl<>(entities);
53+
when(userRepository.findAll(any(Pageable.class))).thenReturn(page);
54+
List<User> result = userService.readUsers(Pageable.unpaged());
55+
assertNotNull(result);
56+
assertEquals(1, result.size());
57+
assertEquals("ID123", result.get(0).getIdentificationNumber());
58+
}
59+
}

0 commit comments

Comments
 (0)