@CreatedDate, @LastModifiedDate์ JPA์์ ์ฒ๋ฆฌํด์ฃผ๋ ๋ ์๋ค์ด๋ผ์ ํ ์คํธ ์์ฑ์ ์ด๋ป๊ฒ ํ ์คํธํด์ผ ํ ๊น ๊ณ ๋ฏผ์ด ๋ง์๋ค. (ํ์ฌ @CreatedDate, @LastModifiedDate๋ BaseEntity์ ์กด์ฌํ๋ฉฐ, ํ ์คํธํ ๋ ์ค๋ฅด์ง ์์๋ค..)
`public static OrderCancelReturnDetailResponseDto of(Order order, List<OrderDetail> orderDetails, String memberShip) {
return OrderCancelReturnDetailResponseDto.builder()
.orderDate(order.getCreatedAt())
.cancelDate(order.getUpdatedAt())
.orderNo(order.getOrderNo())
.cancelReason(orderDetails.get(0).getReason())
.productListResponseDtos(
orderDetails.stream()
.map(ProductListResponseDto::of)
.toList()
)
.cancelRefundInfoResponseDto(
CancelRefundInfoResponseDto.of(orderDetails, memberShip)
)
.build();
}
์ด๋ฐ ์ฝ๋๋ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑ ํ ๋ Date์ ๋ํ ํ ์คํธ๋ ๊ฑฐ์ ๋ถ๊ฐ๋ฅ์ ๊ฐ๊น๋ค. Mockito, Mockbean๋ฑ์ ํตํด ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ๋ฉด ๋ ํ ์ง๋ง ์ด๋ฌํ ๋ฐฉ๋ฒ๋ค์ ์ต๋ํ ์ง์ํ๋ ์ ์ฅ์์ ์ด๋ป๊ฒ ํด์ผํ ์ง ๊ณ ๋ฏผ์ด ์๊ฒผ๋ค.
์๊ฐ์ ๋ํ ๊ฒ์ ์ธ๋ถ์์ ์ ๊ณต๋ฐ๋ ๊ตฌ์กฐ๋ก ๋ฐ๊ฟ์ผ ํ๋ค๊ณ ์๊ฐํ๋ค.
public static OrderCancelReturnDetailResponseDto of(Order order, List<OrderDetail> orderDetails, String memberShip, LocalDateTime createTime, LocalDateTime updateTime) {
return OrderCancelReturnDetailResponseDto.builder()
.orderDate(createTime)
.cancelDate(updateTime)
.orderNo(order.getOrderNo())
.cancelReason(orderDetails.get(0).getReason())
.productListResponseDtos(
orderDetails.stream()
.map(ProductListResponseDto::of)
.toList()
)
.cancelRefundInfoResponseDto(
CancelRefundInfoResponseDto.of(orderDetails, memberShip)
)
.build();
}
์ด๋ ๊ฒ ํ๋ฉด ํ ์คํธ๊ฐ ์ด๋์ ๋ ๊ฐ๋ฅํด์ง ๊ฒ์ผ๋ก ์๊ฐ๋์ง๋ง ๋ณด๊ธฐ ์ข์ง ์์๋ค.
public static OrderCancelReturnDetailResponseDto of(Order order, List<OrderDetail> orderDetails, String memberShip, DateTimeHolder dateTimeHolder) {
return OrderCancelReturnDetailResponseDto.builder()
.orderDate(dateTimeHolder.getCreateTime(order))
.cancelDate(dateTimeHolder.getUpdateTime(order))
.orderNo(order.getOrderNo())
.cancelReason(orderDetails.get(0).getReason())
.productListResponseDtos(
orderDetails.stream()
.map(ProductListResponseDto::of)
.toList()
)
.cancelRefundInfoResponseDto(
CancelRefundInfoResponseDto.of(orderDetails, memberShip)
)
.build();
}
ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ์ํํ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ผ ์ข์ ๊ตฌ์กฐ๋ก ๊ฐ ์ ์๋ค๋ ์๊ฐ์ ํ๊ฒ๋๋ค.
์ข ๋ ์์ธํ DateTimeHolder๋ฅผ ๋ณด๋ฉด
public interface DateTimeHolder {
LocalDateTime getTimeNow();
LocalDateTime getCreateTime(BaseEntity entity);
LocalDateTime getUpdateTime(BaseEntity entity);
LocalDateTime getDeleteTime(BaseEntity entity);
}
@Component
public class SystemDataTimeHolder implements DateTimeHolder {
@Override
public LocalDateTime getTimeNow() {
return LocalDateTime.now();
}
@Override
public LocalDateTime getCreateTime(BaseEntity entity) {
return entity.getCreatedAt();
}
@Override
public LocalDateTime getUpdateTime(BaseEntity entity) {
return entity.getUpdatedAt();
}
@Override
public LocalDateTime getDeleteTime(BaseEntity entity) {
return entity.getDeletedAt();
}
}
๋ค์๊ณผ ๊ฐ์ด ๊ตฌ์ฑํ์ฌ Service์์ ์ฃผ์ ๋ฐ์ ์ฌ์ฉํ ์ ์๊ฒ ํ์๋ค.
์ด ๊ฒฐ๊ณผ ํ
์คํธ๋ฅผ ์์ฑํ๋๋ฐ ์ข์ ๊ตฌ์กฐ๋ก ๋ฐ๋๊ฒ ๋์๋ค.
ํ
์คํธ๋ฅผ ์์ฑํ๊ธฐ ์ DateTimeHolder๋ฅผ mockingํ ํ
์คํธ์ฉ ๊ฐ์ฒด๊ฐ ํ์๋ก ํ๋ค.
public class TestDateTimeHolder implements DateTimeHolder {
private LocalDateTime now;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private LocalDateTime deleteTime;
@Builder
private TestDateTimeHolder(LocalDateTime now, LocalDateTime createTime, LocalDateTime updateTime, LocalDateTime deleteTime) {
this.now = now;
this.createTime = createTime;
this.updateTime = updateTime;
this.deleteTime = deleteTime;
}
@Override
public LocalDateTime getTimeNow() {
return now;
}
@Override
public LocalDateTime getCreateTime(BaseEntity entity) {
return createTime;
}
@Override
public LocalDateTime getUpdateTime(BaseEntity entity) {
return updateTime;
}
@Override
public LocalDateTime getDeleteTime(BaseEntity entity) {
return deleteTime;
}
}
LocalDateTime createTime = LocalDateTime.of(2024, 1, 30, 2, 15);
LocalDateTime updateTime = LocalDateTime.of(2024, 1, 30, 2, 20);
TestDateTimeHolder timeHolder = TestDateTimeHolder.builder()
.createTime(createTime)
.updateTime(updateTime)
.build();
// when
OrderCancelReturnDetailResponseDto result = OrderCancelReturnDetailResponseDto.of(order, orderDetails, WOW.getText(), timeHolder);
// then
assertThat(result).extracting("orderDate", "cancelDate", "orderNo", "cancelReason")
.contains(createTime, updateTime, "123", "๋จ์๋ณ์ฌ");
์ด๋ฐ์์ผ๋ก ์ฃผ์ ๋ฐ์์ ์ฌ์ฉํ๋ฉด @Mockbean, Mockito๋ฑ์ ์ฌ์ฉํ์ง ์์๋ ํ ์คํธ๊ฐ ๊ฐ๋ฅํด์ง๋ค!!!
๊ณ ๋ฏผ์ ๊ฒฐ๊ณผ๊ฐ ๋์์ ๊ธฐ๋ถ์ด ๋งค์ฐ ์ข์๋ฒ๋ฆฐ๋ค~!~!