|
| 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