BaseTimeEntity.java
@Getter
@MappedSuperclass // 1
@EntityListeners(AuditingEntityListener.class) // 2
public abstract class BaseTimeEntity {
@CreatedDate // 3
private LocalDateTime createdDate;
@LastModifiedDate // 4
private LocalDateTime modifiedDate;
}
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@CreatedDate
@LastModifiedDate
@EnableJpaAuditing
@RunWith(SpringRunner.class)
@SpringBootTest
public class PostsRepositoryTest {
@Autowired
PostsRepository postsRepository;
@After
public void cleanup() {
postsRepository.deleteAll();
}
@Test
public void BaseTimeEntity_등록() {
//given
LocalDateTime now = LocalDateTime.of(2019, 6, 4, 0, 0, 0);
postsRepository.save(Posts.builder()
.title("title")
.content("content")
.author("author")
.build());
//when
List<Posts> postsList = postsRepository.findAll();
//then
Posts posts = postsList.get(0);
System.out.println(">>>>> createDate="+posts.getCreatedDate()+", modifiedDate="+posts.getModifiedDate());
assertThat(posts.getCreatedDate()).isAfter(now);
Assertions.assertThat(posts.getModifiedDate()).isAfter(now);
}
}