I created the following Entity and test it using h2:
@Getter
public class Topic {
@Id
private long id;
private final Title title;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime lastModified;
// ...
}
The TopicRepository
is an empty interface.
The following test fails with the error that createdAt
is null:
@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());
}
}
My Application is annotated with @SpringBootApplication
and @EnableJdbcAuditing
.
Why is createdAt
still null
, lastModified
on the other hand is not null?
Edit
I changed the types of Topic.createdAt
and Topic.lastModified
to Instant
, which did not work.
Also, I added the following method, which, I guess, should provide values for the Instant
fields:
@Bean
public AuditorAware<Instant> instantAuditorAware() {
return () -> Optional.of(Instant.now());
}
Sadly, although the method gets called, createdAt
is still null
.