我创建了以下实体并使用 h2 对其进行测试:
@Getter
public class Topic {
@Id
private long id;
private final Title title;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime lastModified;
// ...
}
The TopicRepository
是一个空接口。
以下测试失败并出现以下错误createdAt
一片空白:
@RunWith(SpringRunner.class)
@SpringBootTest
public class BasicRepositoryTests {
@Autowired
TopicRepository topicRepository;
@Test
public void topicRepositoryWorks() {
val topic = new Topic();
val savedTopic = topicRepository.save(topic);
assertEquals(1, topicRepository.count());
assertNotNull(savedTopic.getLastModified(), "lastModified must be set");
assertNotNull(savedTopic.getCreatedAt(), "createdAt must be set");
topicRepository.delete(savedTopic);
assertEquals(0, topicRepository.count());
}
}
我的应用程序注释为@SpringBootApplication
and @EnableJdbcAuditing
.
Why is createdAt
still null
, lastModified
另一方面不为空?
Edit
我改变了类型Topic.createdAt
and Topic.lastModified
to Instant
,这不起作用。
另外,我添加了以下方法,我想它应该为Instant
fields:
@Bean
public AuditorAware<Instant> instantAuditorAware() {
return () -> Optional.of(Instant.now());
}
可悲的是,尽管该方法被调用,createdAt
还是null
.