Last active
June 30, 2022 13:22
-
-
Save atesibrahim/cb186a915f0c3cfcc133c6062b9552c0 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 ProductControllerTest { | |
@InjectMocks | |
private ProductController productController; | |
@Mock | |
private ProductService productService; | |
private MockMvc mockMvc; | |
private final ObjectMapper objectMapper = new ObjectMapper(); | |
@BeforeEach | |
public void setup() { | |
mockMvc = MockMvcBuilders.standaloneSetup(productController).build(); | |
} | |
@Test | |
public void it_should_test_when_product_id_is_valid() throws Exception { | |
//given | |
ProductRequest productRequest = ProductRequest.builder().id(1).build(); | |
ArgumentCaptor<ProductRequest> argumentCaptor = ArgumentCaptor.forClass(ProductRequest.class); | |
//when | |
ResultActions result = mockMvc.perform(post("/products") | |
.content(objectMapper.writeValueAsString(productRequest)) | |
.contentType(MediaType.APPLICATION_JSON)); | |
//then | |
verify(productService).dispense(argumentCaptor.capture()); | |
result.andExpect(status().isOk()); | |
} | |
@Test | |
public void it_should_test_when_product_id_is_not_valid() throws Exception { | |
//given | |
ProductRequest productRequest = ProductRequest.builder().id(0).build(); | |
//when | |
ResultActions result = mockMvc.perform(post("/products") | |
.content(objectMapper.writeValueAsString(productRequest)) | |
.contentType(MediaType.APPLICATION_JSON)); | |
//then | |
verifyNoInteractions(productService); | |
result.andExpect(status().isBadRequest()); | |
} | |
@Test | |
public void it_should_test_when_product_id_is_null() throws Exception { | |
//given | |
ProductRequest productRequest = ProductRequest.builder().build(); | |
//when | |
ResultActions result = mockMvc.perform(post("/products") | |
.content(objectMapper.writeValueAsString(productRequest)) | |
.contentType(MediaType.APPLICATION_JSON)); | |
//then | |
verifyNoInteractions(productService); | |
result.andExpect(status().isBadRequest()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment