Last active
June 30, 2022 13:24
-
-
Save atesibrahim/7ce61df1036427df59d41d78142c0afc to your computer and use it in GitHub Desktop.
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
@ExtendWith(SpringExtension.class) | |
class ProductServiceImplTest { | |
@InjectMocks | |
private ProductServiceImpl productService; | |
@Mock | |
private ProductRepository productRepository; | |
@Mock | |
private CoinAmount coinAmount; | |
@Test | |
public void it_should_return_dispense_product_and_thank_you() { | |
//Given | |
Integer id = 1; | |
Product product = Product.builder().id(1).name("name").price(5.0).stock(10).build(); | |
ProductRequest productRequest = ProductRequest.builder().id(1).build(); | |
//when | |
when(coinAmount.getBalance()).thenReturn(10.0); | |
when(productRepository.findById(id)).thenReturn(Optional.ofNullable(product)); | |
ProductDispenseResponse result = productService.dispense(productRequest); | |
//Then | |
verify(productRepository).findById(id); | |
assertThat(result.getProductName()).isEqualTo("name"); | |
assertThat(result.getResponseMessage()).isEqualTo("THANK YOU"); | |
assertThat(result.getCurrentBalance()).isEqualTo(10.0); | |
} | |
@Test | |
public void it_should_return_sold_out_when_out_of_stock() { | |
//Given | |
Integer id = 1; | |
Product product = Product.builder().id(1).name("name").price(5.0).stock(0).build(); | |
ProductRequest productRequest = ProductRequest.builder().id(1).build(); | |
//when | |
when(coinAmount.getBalance()).thenReturn(10.0); | |
when(productRepository.findById(id)).thenReturn(Optional.ofNullable(product)); | |
ProductDispenseResponse result = productService.dispense(productRequest); | |
//Then | |
verify(productRepository).findById(id); | |
assertThat(result.getResponseMessage()).isEqualTo("SOLD OUT"); | |
assertThat(result.getCurrentBalance()).isEqualTo(10.0); | |
} | |
@Test | |
public void it_should_return_unsufficient_message_when_balance_not_enough() { | |
//Given | |
Integer id = 1; | |
Product product = Product.builder().id(1).name("name").price(5.0).stock(3).build(); | |
ProductRequest productRequest = ProductRequest.builder().id(1).build(); | |
//when | |
when(coinAmount.getBalance()).thenReturn(1.0); | |
when(productRepository.findById(id)).thenReturn(Optional.ofNullable(product)); | |
ProductDispenseResponse result = productService.dispense(productRequest); | |
//Then | |
verify(productRepository).findById(id); | |
assertThat(result.getResponseMessage()).isEqualTo("Unfortunately your balance is not sufficient."); | |
assertThat(result.getCurrentBalance()).isEqualTo(1.0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment