|
23 | 23 | import org.junit.jupiter.api.Test; |
24 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
25 | 25 | import org.springframework.boot.test.context.SpringBootTest; |
26 | | -import org.springframework.transaction.annotation.Transactional; |
27 | 26 |
|
28 | 27 | @SpringBootTest |
29 | | -@Transactional |
30 | 28 | class OrderFacadeIntegrationTest { |
31 | 29 |
|
32 | 30 | @Autowired |
@@ -54,14 +52,14 @@ class PlaceOrder { |
54 | 52 | void placeOrder_success() { |
55 | 53 | // arrange |
56 | 54 | User user = new User("userA", "a@email.com", "2025-11-11", Gender.MALE, new Point(100000L)); |
57 | | - userRepository.save(user); |
| 55 | + User savedUser = userRepository.save(user); |
58 | 56 |
|
59 | 57 | Long brandId = 1L; |
60 | 58 | Product product = new Product(brandId, "Product A", "설명", new Money(20000L), 10); |
61 | 59 | Product saveProduct = productRepository.save(product); |
62 | 60 |
|
63 | 61 | Item itemCommand = new Item(saveProduct.getId(), 3); |
64 | | - OrderCommand.PlaceOrder command = new OrderCommand.PlaceOrder(user.getId(), List.of(itemCommand)); |
| 62 | + OrderCommand.PlaceOrder command = new OrderCommand.PlaceOrder(savedUser.getId(), List.of(itemCommand)); |
65 | 63 |
|
66 | 64 | // act |
67 | 65 | OrderInfo result = orderFacade.placeOrder(command); |
@@ -122,5 +120,33 @@ void placeOrder_fail_insufficient_point() { |
122 | 120 |
|
123 | 121 | assertThat(exception.getErrorType()).isEqualTo(ErrorType.BAD_REQUEST); |
124 | 122 | } |
| 123 | + |
| 124 | + @DisplayName("포인트 잔액 부족으로 주문이 실패하면, 차감되었던 재고는 롤백되어 원상복구된다.") |
| 125 | + @Test |
| 126 | + void placeOrder_transaction_rollback_test() { |
| 127 | + // arrange |
| 128 | + Product product = productRepository.save( |
| 129 | + new Product(1L, "Product A", "설명", new Money(20000L), 10) |
| 130 | + ); |
| 131 | + |
| 132 | + User user = userRepository.save( |
| 133 | + new User("userRollback", "rollback@email.com", "2025-11-11", Gender.MALE, new Point(0L)) |
| 134 | + ); |
| 135 | + |
| 136 | + Item itemCommand = new Item(product.getId(), 1); // 1개 주문 시도 |
| 137 | + OrderCommand.PlaceOrder command = new OrderCommand.PlaceOrder(user.getId(), List.of(itemCommand)); |
| 138 | + |
| 139 | + // act |
| 140 | + assertThrows(CoreException.class, () -> { |
| 141 | + orderFacade.placeOrder(command); |
| 142 | + }); |
| 143 | + |
| 144 | + // assert |
| 145 | + Product rollbackedProduct = productRepository.findById(product.getId()).orElseThrow(); |
| 146 | + |
| 147 | + assertThat(rollbackedProduct.getStock()).isEqualTo(10); |
| 148 | + } |
125 | 149 | } |
| 150 | + |
| 151 | + |
126 | 152 | } |
0 commit comments