Skip to content

Commit 2e24056

Browse files
committed
test : createdAt 을 설정하는 DomainUtils 구현
1 parent 48ad47d commit 2e24056

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/main/java/eatda/domain/AuditingEntity.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public abstract class AuditingEntity {
1515

1616
@PrePersist
1717
protected void onCreate() {
18-
this.createdAt = LocalDateTime.now();
18+
if (createdAt == null) {
19+
this.createdAt = LocalDateTime.now();
20+
}
1921
}
2022
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package eatda.util;
2+
3+
import eatda.domain.AuditingEntity;
4+
import java.time.LocalDateTime;
5+
import org.springframework.test.util.ReflectionTestUtils;
6+
7+
public final class DomainUtils {
8+
9+
private static final String CREATED_AT_FIELD = "createdAt";
10+
11+
private DomainUtils() {
12+
}
13+
14+
public static <T extends AuditingEntity> void setCreatedAt(T entity, LocalDateTime createdAt) {
15+
try {
16+
ReflectionTestUtils.setField(entity, CREATED_AT_FIELD, createdAt);
17+
} catch (Exception e) {
18+
throw new IllegalArgumentException("Failed to set createdAt field", e);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)