Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to bypass AuditingEntityListener to set up data for a test?

We've got a Spring project that is using the AuditingEntityListener on our JPA entities:

@EntityListeners(AuditingEntityListener.class)

Our base entity has a lastModifiedDate defined as:

@Column(name = "modified_time")
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
private Date lastModifiedDate;

This value is being set automatically when the entity is saved or updated - which is how we'd like the application to behave. We run into issues, though, when we try to set up data in our test suites because in some situations (not all), we'd like to bypass the automatic setting of this field and set the value ourself. In this specific situation, we're trying to order a bunch of test data and then run a test against it.

Is there any way to bypass or turn off the AuditingEntityListener in order to create test data?

like image 525
Chris Williams Avatar asked Sep 13 '25 08:09

Chris Williams


1 Answers

Declaring

@MockBean
private AuditingHandler auditingHandler

in your test should prevent the @LastModifiedDate from having any effect.

like image 193
ryanp Avatar answered Sep 16 '25 00:09

ryanp