Skip to content

Commit 6aebe86

Browse files
committed
test: add unit test for PotsService
1 parent 8d53454 commit 6aebe86

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/main/java/fun/trackmoney/pots/dtos/PotsResponseDTO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fun.trackmoney.pots.dtos;
22

3-
public record PotsResponseDTO(String name,
3+
public record PotsResponseDTO(Long potId,
4+
String name,
45
String description,
56
Long targetAmount,
67
Long currentAmount) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package fun.trackmoney.pots.service;
2+
3+
import fun.trackmoney.account.entity.AccountEntity;
4+
import fun.trackmoney.pots.dtos.PotsResponseDTO;
5+
import fun.trackmoney.pots.entity.PotsEntity;
6+
import fun.trackmoney.pots.mapper.PotsMapper;
7+
import fun.trackmoney.pots.repository.PotsRepository;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.mockito.InjectMocks;
11+
import org.mockito.Mock;
12+
import org.mockito.MockitoAnnotations;
13+
14+
import java.util.List;
15+
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.mockito.Mockito.when;
19+
20+
class PotsServiceTest {
21+
22+
@InjectMocks
23+
private PotsService potsService;
24+
25+
@Mock
26+
private PotsRepository potsRepository;
27+
28+
@Mock
29+
private PotsMapper potsMapper;
30+
31+
@BeforeEach
32+
void setUp() {
33+
MockitoAnnotations.openMocks(this);
34+
}
35+
36+
@Test
37+
void findAllPots_shouldReturnMappedPotsList() {
38+
Integer accountId = 1;
39+
PotsEntity pots1 = new PotsEntity(1L,"test name2", "Test Pot1", 100L, 1001L, new AccountEntity());
40+
PotsEntity pots2 = new PotsEntity(2L,"test name2", "Test Pot2", 100L, 1002L, new AccountEntity());
41+
42+
List<PotsEntity> potsEntities = List.of(pots1, pots2);
43+
44+
List<PotsResponseDTO> potsResponseDTOList = List.of(
45+
new PotsResponseDTO(1L, "test name2", "Test Pot1", 100L, 1001L),
46+
new PotsResponseDTO(2L, "test name2", "Test Pot2", 100L, 1002L)
47+
);
48+
49+
when(potsRepository.findAllPotsByAccountId(accountId)).thenReturn(potsEntities);
50+
when(potsMapper.listToResponse(potsEntities)).thenReturn(potsResponseDTOList);
51+
52+
var result = potsService.findAllPots(accountId);
53+
54+
assertNotNull(result);
55+
assertEquals(2, result.size());
56+
assertEquals(1L, result.get(0).potId());
57+
}
58+
}

0 commit comments

Comments
 (0)