forked from KBTG-Kampus-ClassNest-SE-Java/workshopb2-group-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor cart service and controller
Add unit test for cart service
- Loading branch information
Showing
3 changed files
with
65 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
kbazaar/src/test/java/com/kampus/kbazaar/cart/CartServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.kampus.kbazaar.cart; | ||
|
||
import com.kampus.kbazaar.cartItem.CartItem; | ||
import com.kampus.kbazaar.cartItem.CartItemService; | ||
import com.kampus.kbazaar.exceptions.NotFoundException; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.when; | ||
|
||
class CartServiceTest { | ||
|
||
@Mock private CartRepository cartRepository; | ||
@Mock private CartItemService cartItemService; | ||
|
||
@InjectMocks private CartService cartService; | ||
|
||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void shouldBeThrowNotFoundExceptionWhenCardEmpty(){ | ||
//Arrange | ||
when(cartRepository.findAll()).thenReturn(List.of()); | ||
|
||
//Act & Assert | ||
assertThrows(NotFoundException.class,() -> cartService.getAll()); | ||
|
||
} | ||
|
||
@Test | ||
void shouldBeAbleToListCartResponse(){ | ||
//Arrange | ||
String username = "TechNinja"; | ||
Cart cart = new Cart(); | ||
cart.setUsername(username); | ||
when(cartRepository.findAll()).thenReturn(List.of(cart)); | ||
|
||
CartItem cartItem = new CartItem(1L,"TechNinja","BEV-COCA-COLA","Coca-Cola",BigDecimal.valueOf(10),BigDecimal.valueOf(10),BigDecimal.valueOf(10),"FIXEDAMOUNT10"); | ||
|
||
when(cartItemService.findByUsername(username)).thenReturn(List.of(cartItem)); | ||
|
||
//Act | ||
List<CartResponse> actualResult = cartService.getAll(); | ||
|
||
//Assert | ||
assertEquals(username,actualResult.get(0).getUsername()); | ||
|
||
|
||
} | ||
} |