| 
11 | 11 | import org.springframework.boot.test.context.SpringBootTest;  | 
12 | 12 | import org.springframework.http.MediaType;  | 
13 | 13 | import org.springframework.test.web.reactive.server.WebTestClient;  | 
 | 14 | +import reactor.core.publisher.Mono;  | 
 | 15 | + | 
 | 16 | +import java.math.BigDecimal;  | 
 | 17 | + | 
 | 18 | +import static org.assertj.core.api.Assertions.assertThat;  | 
14 | 19 | 
 
  | 
15 | 20 | @SpringBootTest  | 
16 | 21 | @AutoConfigureWebTestClient  | 
@@ -63,6 +68,47 @@ void testGetProductByIdNotFound() {  | 
63 | 68 |  .expectStatus().isNotFound();  | 
64 | 69 |  }  | 
65 | 70 | 
 
  | 
 | 71 | + @Test  | 
 | 72 | + @Order(4)  | 
 | 73 | + void testCreateProduct() {  | 
 | 74 | + // Given  | 
 | 75 | + ProductDTO newProductDTO = new ProductDTO();  | 
 | 76 | + newProductDTO.setName("New Product");  | 
 | 77 | + newProductDTO.setDescription("New Product");  | 
 | 78 | + newProductDTO.setImgUrl("https://image.com/image.jpeg");  | 
 | 79 | + newProductDTO.setPrice(BigDecimal.TEN);  | 
 | 80 | + // Set other fields as needed  | 
 | 81 | + | 
 | 82 | + // When  | 
 | 83 | + webTestClient.post().uri(ProductController.PRODUCT_BASE_URL)  | 
 | 84 | + .body(Mono.just(newProductDTO), ProductDTO.class)  | 
 | 85 | + .exchange()  | 
 | 86 | + // Then  | 
 | 87 | + .expectStatus().isCreated()  | 
 | 88 | + .expectHeader().contentType(MediaType.APPLICATION_JSON)  | 
 | 89 | + .expectBody(ProductDTO.class)  | 
 | 90 | + .value(created -> {  | 
 | 91 | + assertThat(created.getName().equals(newProductDTO.getName())).isTrue();  | 
 | 92 | + }); // Define expectedProductDTO based on test data  | 
 | 93 | + }  | 
 | 94 | + | 
 | 95 | + @Test  | 
 | 96 | + void testCreateProductBadRequest() {  | 
 | 97 | + // Given  | 
 | 98 | + ProductDTO invalidProductDTO = new ProductDTO();  | 
 | 99 | + invalidProductDTO.setName(null); // Set name to null to simulate a bad request  | 
 | 100 | + invalidProductDTO.setImgUrl("https://image.com/image.jpeg");  | 
 | 101 | + invalidProductDTO.setDescription("New Product");  | 
 | 102 | + invalidProductDTO.setPrice(BigDecimal.TEN);  | 
 | 103 | + // Set other fields as needed  | 
 | 104 | + | 
 | 105 | + // When  | 
 | 106 | + webTestClient.post().uri(ProductController.PRODUCT_BASE_URL)  | 
 | 107 | + .body(Mono.just(invalidProductDTO), ProductDTO.class)  | 
 | 108 | + .exchange()  | 
 | 109 | + // Then  | 
 | 110 | + .expectStatus().isBadRequest();  | 
 | 111 | + }  | 
66 | 112 | 
 
  | 
67 | 113 |  private ProductDTO getSavedTestProduct() {  | 
68 | 114 |  return productService.getAllProducts().next().block();  | 
 | 
0 commit comments