3030import org .mockito .InjectMocks ;
3131import org .mockito .Mock ;
3232import org .mockito .junit .jupiter .MockitoExtension ;
33+ import org .springframework .context .ApplicationEventPublisher ;
3334import org .springframework .data .domain .PageRequest ;
3435import org .springframework .data .domain .SliceImpl ;
3536
@@ -46,15 +47,17 @@ class ProductServiceTest {
4647 @ Mock
4748 private KeywordGenerationService keywordGenerationService ;
4849
50+ @ Mock
51+ private ApplicationEventPublisher eventPublisher ; // Add mock for event publisher
52+
4953 @ InjectMocks
5054 private ProductService productService ;
5155
52- // Helper method to create a Store object with ID for testing
5356 private Store createTestStore (Long id , Long sellerProfileId , String name ) {
5457 Store store = Store .builder ()
5558 .sellerProfileId (sellerProfileId )
5659 .name (name )
57- .storeCategory (null ) // Not relevant for product tests
60+ .storeCategory (null )
5861 .roadAddr ("Test Road" )
5962 .imageUrl ("test.jpg" )
6063 .location (null )
@@ -69,7 +72,6 @@ private Store createTestStore(Long id, Long sellerProfileId, String name) {
6972 return store ;
7073 }
7174
72- // Helper method to create a Product object with ID for testing
7375 private Product createTestProduct (Long id , Store store , String name , String description , Integer price , String imageUrl , Integer initialStock ) {
7476 Product product = Product .builder ()
7577 .store (store )
@@ -100,7 +102,7 @@ void createProduct_success() {
100102 Product savedProductMock = mock (Product .class );
101103 when (savedProductMock .getId ()).thenReturn (1L );
102104 when (savedProductMock .getName ()).thenReturn (request .name ());
103- when (savedProductMock .getStore ()).thenReturn (store ); // Mock the store for ProductResponse.from
105+ when (savedProductMock .getStore ()).thenReturn (store );
104106
105107 when (storeService .getStoreById (storeId )).thenReturn (store );
106108 when (productRepository .save (any (Product .class ))).thenReturn (savedProductMock );
@@ -124,18 +126,18 @@ void deleteProduct_success() {
124126 Long storeId = 1L ;
125127 Long productId = 1L ;
126128 Store store = createTestStore (storeId , 1L , "Test Store" );
127- Product product = mock (Product .class ); // Make product a mock
128- // when(product.getId()).thenReturn(productId); // Not directly used by deleteProduct or validateStore
129+ Product product = mock (Product .class );
129130
130131 when (productRepository .getById (productId )).thenReturn (product );
131- doNothing ().when (productRepository ).delete (any (Product .class ));
132+ when (product .getStore ()).thenReturn (store );
133+ doNothing ().when (productRepository ).delete (product );
132134
133135 // when
134136 productService .deleteProduct (storeId , productId );
135137
136138 // then
137139 verify (productRepository ).getById (productId );
138- verify (product ).validateStore (storeId ); // Verify interaction with the mock product
140+ verify (product ).validateStore (storeId );
139141 verify (productRepository ).delete (product );
140142 }
141143
@@ -147,8 +149,7 @@ void deleteProduct_fail_storeIdMismatch() {
147149 Long productId = 1L ;
148150 Long wrongStoreId = 2L ;
149151 Store store = createTestStore (storeId , 1L , "Test Store" );
150- Product product = mock (Product .class ); // Make product a mock
151- // when(product.getId()).thenReturn(productId); // Not directly used by deleteProduct or validateStore
152+ Product product = mock (Product .class );
152153
153154 when (productRepository .getById (productId )).thenReturn (product );
154155 doThrow (new CustomException (ErrorCode .PRODUCT_STORE_MISMATCH ))
@@ -160,7 +161,7 @@ void deleteProduct_fail_storeIdMismatch() {
160161 assertThat (exception .getCode ()).isEqualTo (ErrorCode .PRODUCT_STORE_MISMATCH .getCode ());
161162 verify (productRepository ).getById (productId );
162163 verify (productRepository , never ()).delete (any (Product .class ));
163- verify (product ).validateStore (wrongStoreId ); // Verify interaction with the mock product
164+ verify (product ).validateStore (wrongStoreId );
164165 }
165166
166167 @ Test
0 commit comments