I have an entity that has a field annotated with @CreationTimestamp:
@Entity
@Table(name = "foo")
class Foo {
...
@Column
@CreationTimestamp
private Instant createdAt;
}
And now I have integration tests where I need to create a few entries with different createdAt timestamps timestamp. But when I set the value to one that I need and save it's getting overridden with the creating timestamp of the VM:
Foo foo = new Foo();
foo.setCreatedAt(Instant.MIN);
foo = fooRepo.save(foo);
foo.getCreatedAt(); // equals to the creation time
How can I insert the desired values for this field in my test env?
I don't think it is possible to disable this behavior in an integration test as you need here.
However you can achieve the same using a workaround by setting the creation date after saving the entity, then save the entity once more to persist the new desired creation time.
This will work since @CreationTimestamp will only be triggered when saving the entity for the first time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With