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
@RestController | |
@RequestMapping("/products") | |
@RequiredArgsConstructor | |
public class ProductController { | |
private final ProductService productService; | |
@PostMapping | |
public ProductDispenseResponse dispense(@Valid @RequestBody ProductRequest productRequest) { | |
return productService.dispense(productRequest); |
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
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> |
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
@Test | |
public void it_should_save_number_when_flag_is_true() { | |
//Given | |
ArgumentCaptor<Product> productArgumentCaptor = ArgumentCaptor.forClass(Product.class); | |
Product product = Product.builder().isFlag(true).build(); | |
//When | |
productService.save(product); | |
//Then |
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; |
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 |
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) | |
public class ProductRepositoryTest { | |
@Autowired | |
private ProductRepository productRepository; | |
@Autowired | |
private TestEntityManager testEntityManager; | |
@Test |
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
@Repository | |
public interface ProductRepository extends JpaRepository<Product, Integer> { | |
@Transactional | |
@Modifying | |
@Query(value = "update product " + | |
"set name = :name " + | |
"where id = :productId", nativeQuery = true) | |
void updateName(@Param("productId") Integer productId, @Param("name") String name); | |
} |
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
@Service | |
public class ProductServiceImpl implements ProductService { | |
@Autowired | |
ProductRepository productRepository; | |
@Autowired | |
CoinAmount coinAmount; | |
@Override |
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
<dependency> | |
<groupId>com.h2database</groupId> | |
<artifactId>h2</artifactId> | |
<scope>test</scope> | |
</dependency> |
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
@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 |
OlderNewer