From b9e59795e62cdf943af6a22e0608d2a88e092d84 Mon Sep 17 00:00:00 2001 From: Umjiseung <127853946+Umjiseung@users.noreply.github.com> Date: Sun, 13 Oct 2024 12:08:42 +0900 Subject: [PATCH] add :: GetMyOwnedStockServiceImplTest --- .../user/GetMyOwnedStockServiceImplTest.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/test/java/com/juicycool/backend/user/GetMyOwnedStockServiceImplTest.java diff --git a/src/test/java/com/juicycool/backend/user/GetMyOwnedStockServiceImplTest.java b/src/test/java/com/juicycool/backend/user/GetMyOwnedStockServiceImplTest.java new file mode 100644 index 0000000..38c9ec4 --- /dev/null +++ b/src/test/java/com/juicycool/backend/user/GetMyOwnedStockServiceImplTest.java @@ -0,0 +1,73 @@ +package com.juicycool.backend.user; + +import com.juicycool.backend.domain.stock.OwnedStocks; +import com.juicycool.backend.domain.stock.repository.OwnedStocksRepository; +import com.juicycool.backend.domain.user.User; +import com.juicycool.backend.domain.user.converter.UserConverter; +import com.juicycool.backend.domain.user.presentation.dto.response.GetMyBoardResponseDto; +import com.juicycool.backend.domain.user.presentation.dto.response.GetMyOwnedStockResponseDto; +import com.juicycool.backend.domain.user.service.impl.GetMyOwnedStockServiceImpl; +import com.juicycool.backend.domain.user.util.UserUtil; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.*; + +public class GetMyOwnedStockServiceImplTest { + + @Mock + private OwnedStocksRepository ownedStocksRepository; + + @Mock + private UserUtil userUtil; + + @Mock + private UserConverter userConverter; + + @InjectMocks + private GetMyOwnedStockServiceImpl getMyOwnedStockService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + @DisplayName("만약 내가 가지고 있는 주식 리스트가 불러와진 경우") + void If_list_of_share_have_is_imported() { + User user = new User(); + + when(userUtil.getCurrentUser()).thenReturn(user); + + OwnedStocks ownedStocks1 = new OwnedStocks(); + OwnedStocks ownedStocks2 = new OwnedStocks(); + + List ownedStocksList = Arrays.asList(ownedStocks1, ownedStocks2); + + GetMyOwnedStockResponseDto dto1 = GetMyOwnedStockResponseDto.builder().build(); + GetMyOwnedStockResponseDto dto2 = GetMyOwnedStockResponseDto.builder().build(); + + List dtoList = Arrays.asList(dto1, dto2); + + when(ownedStocksRepository.findByUser(user)).thenReturn(ownedStocksList); + when(userConverter.toDto(ownedStocks1)).thenReturn(dto1); + when(userConverter.toDto(ownedStocks2)).thenReturn(dto2); + + List result = getMyOwnedStockService.execute(); + + assertEquals(result, dtoList); + when(ownedStocksRepository.findByUser(user)).thenReturn(ownedStocksList); + verify(userUtil, times(1)).getCurrentUser(); + verify(ownedStocksRepository, times(1)).findByUser(user); + verify(userConverter, times(1)).toDto(ownedStocks1); + verify(userConverter, times(1)).toDto(ownedStocks2); + } +}