์ง๋ ๋จ์ ํ ์คํธ ๊ฐ๋ ์ ์ด์ด์ Mockito๋ฅผ ์ฐ์ตํด๋ณด์
productService์ ์๋ update
์ฝ๋๋ฅผ ํ
์คํธํด๋ณด์
@Transactional
public ProductResponseDto updateProduct(Long id, ProductMypriceRequestDto requestDto) {
int myprice = requestDto.getMyprice();
if (myprice < MIN_MY_PRICE) {
throw new IllegalArgumentException("์ ํจํ์ง ์์ ๊ด์ฌ ๊ฐ๊ฒฉ์
๋๋ค. ์ต์ " + MIN_MY_PRICE + "์ ์ด์์ผ๋ก ์ค์ ํด ์ฃผ์ธ์.");
}
Product product = productRepository.findById(id)
.orElseThrow(() -> new NullPointerException("ํด๋น ์ํ์ ์ฐพ์ ์ ์์ต๋๋ค."));
product.update(requestDto);
return new ProductResponseDto(product);
}
์ฌ๊ธฐ์ ๋ค ์ ์๋ ์๋ฌธ
ํ
์คํธ ์ฝ๋์ ์์ฑ์ ์ฃผ์
์ ๋ฃ์ ์ ์๋? repository์ ์๋ findById
๋ ์ด๋ป๊ฒ ๋์ํ์ง?
๋ผ๋ ์๋ฌธ์ด ๋ค ์ ์๋ค
ํ์ง๋ง ์ฐ๋ฆฌ๊ฐ ํ
์คํธ ํ๊ณ ์ถ์๊ฑด findById
๊ฐ ์๋๋ผ ์๋น์ค์ ์๋ update
์ฝ๋ ์์ฒด๊ฐ ์ ์ฒ๋ฆฌ๋๋์ง์ด๋ค
repository๊ฐ ์๋๋ผ service๋ง ํ
์คํธ ํ๊ธฐ ์ํด Mock Object
๋ฅผ ์ฌ์ฉํ ์ ์๋ค
๊ทธ๋ฆฌ๊ณ ๊ทธ Mock Object๋ฅผ ์ ๊ณตํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ Mockito ์ด๋ค
Mockito
@ExtendWith(MockitoExtension.class) // @Mock ์ฌ์ฉ์ ์ํด ์ค์ ํฉ๋๋ค.
class ProductServiceTest {
@Mock
ProductRepository productRepository;
@Mock
FolderRepository folderRepository;
@Mock
ProductFolderRepository productFolderRepository;
...
Mockito๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ๋ฐ๋ก dependency๋ฅผ ์ถ๊ฐํ ํ์๋ ์๋ค
@ExtendWith(MockitoExtension.class)
์ ๋
ธํ
์ด์
์ ๋ฌ์์ ์ค์ ํด์ฃผ๊ณ Mock
์ค๋ธ์ ํธ๋ก ์ฌ์ฉํ ๊ฐ์ง ๊ฐ์ฒด(ํด๋์ค๋ ์ธํฐํ์ด์ค)๋ฅผ @Mock ์ ๋
ธํ
์ด์
์ ๋ฌ์์ฃผ๋ฉด Mockito๊ฐ ๊ฐ์ง๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ค๋ค
@Test
@DisplayName("๊ด์ฌ ์ํ ํฌ๋ง๊ฐ - ์ต์ ๊ฐ ์ด์์ผ๋ก ๋ณ๊ฒฝ")
void test1() {
// given
Long productId = 100L;
int myprice = ProductService.MIN_MY_PRICE + 3_000_000;
ProductMypriceRequestDto requestMyPriceDto = new ProductMypriceRequestDto();
requestMyPriceDto.setMyprice(myprice);
ProductService productService = new ProductService(productRepository, productFolderRepository, folderRepository);
// when
ProductResponseDto result = productService.updateProduct(productId, requestMyPriceDto);
// then
assertEquals(myprice, result.getMyprice());
}
- given
- productId์ myprice๋ฅผ ์์๋ก ๋ฃ์ด์ค๋ค
- setMyprice๋ฅผ ์ด์ฉํด requestMyPriceDto๋ฅผ ์์ฑํด์ค๋ค
- ๊ทธ๋ฆฌ๊ณ productService๋ฅผ ํ๋ ๋ง๋ค์ด์ค๋ค
- when
- ์ค์ ๋ก productService๋ฅผ ์คํํด result๊ฐ์ ๋ฐ๋๋ค
- then
- assertEquals๋ฅผ ์ด์ฉํด ๋ด๊ฐ ๋ฃ์ myprice์ ๊ฒฐ๊ณผ๋ฌผ์ myprice๊ฐ ๊ฐ์์ง ๋น๊ตํด Update๊ฐ ์ ์ํ๋๊ณ ์๋์ง ํ์ธํ๋ค
ํ์ง๋ง ์ด๋๋ก ์คํํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค
๊ฐ์ง ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ์ ๋ฌํด์ฃผ๊ธฐ๋ง ํ๋๊ฒ ์๋๋ผ ์ฌ์ฉ ์ผ์ด์ค๋ฅผ ์ ๋๋ก ์ ์ํด์ ๋๊ฒจ์ค์ผํ๊ธฐ ๋๋ฌธ์ด๋ค
๋ค์ ๋งํ์ง๋ง ์ฐ๋ฆฌ๋ update๊ฐ ์ ๋๋ก ๋๋๊ฐ? ๊ฐ ์ค์ํ ํฌ์ธํธ์ด๋ค
์ฌ์ฉ ์ผ์ด์ค๋ฅผ ์ถ๊ฐํ ์ฝ๋๋ฅผ ๋ค์ ๋ณด์
@Test
@DisplayName("๊ด์ฌ ์ํ ํฌ๋ง๊ฐ - ์ต์ ๊ฐ ์ด์์ผ๋ก ๋ณ๊ฒฝ")
void test1() {
// given
Long productId = 100L;
int myprice = ProductService.MIN_MY_PRICE + 3_000_000;
ProductMypriceRequestDto requestMyPriceDto = new ProductMypriceRequestDto();
requestMyPriceDto.setMyprice(myprice);
User user = new User();
ProductRequestDto requestProductDto = new ProductRequestDto(
"Apple <b>๋งฅ๋ถ</b> <b>ํ๋ก</b> 16ํ 2021๋
<b>M1</b> Max 10์ฝ์ด ์ค๋ฒ (MK1H3KH/A) ",
"https://shopping-phinf.pstatic.net/main_2941337/29413376619.20220705152340.jpg",
"https://search.shopping.naver.com/gate.nhn?id=29413376619",
3515000
);
Product product = new Product(requestProductDto, user);
ProductService productService = new ProductService(productRepository,folderRepository, productFolderRepository);
given(productRepository.findById(productId)).willReturn(Optional.of(product));
// when
ProductResponseDto result = productService.updateProduct(productId, requestMyPriceDto);
// then
assertEquals(myprice, result.getMyprice());
}
product๋ฅผ ์ง์ ๋ง๋ค์ด์ฃผ๋ ๋ถ๋ถ์ด ์ถ๊ฐ๋์๋ค (User, ProductRequestDto)
๊ทธ๋ฆฌ๊ณ Mockito์ given์ด๋ผ๋ method๋ฅผ ์ด์ฉํด ๋ฃ์ด์ค ์ ์๋ค
(ํธ-์)