Spring-boot: @EnableJpaAuditing does not work with @DataJpaTest

Created on 2 Jun 2018  路  5Comments  路  Source: spring-projects/spring-boot

Spring Boot 2.0.2.RELEASE

I have tried to write a Jpa sample test with @DataJpaTest, and I set @CreatedDate and @LastModifiedDate on my entity.

When I run the test, these fields are not set at all. If I replace @DataJpaTest with @SpringBootTest, it worked.

https://github.com/hantsy/springboot-jwt-sample/blob/master/src/test/java/com/example/demo/VehicleJpaTest.java#L26

duplicate

Most helpful comment

Hello @snicoll, from my experience I find that it does work with @SpringBootTest. However you can make it work with @DataJpaTest as well by including your @EnableJpaAuditing class. Mine usually will look something like this:

@DataJpaTest(includeFilters = @Filter(
        type = ASSIGNABLE_TYPE,
        classes = {AuditorAwareImpl.class, AuditConfiguration.class}
))

All 5 comments

It "does not work" either with @SpringBootTest as your example as an explicit @EnableJpaAuditing and we're not auto-configuring this at all.

Duplicate of #6016

Hello @snicoll, from my experience I find that it does work with @SpringBootTest. However you can make it work with @DataJpaTest as well by including your @EnableJpaAuditing class. Mine usually will look something like this:

@DataJpaTest(includeFilters = @Filter(
        type = ASSIGNABLE_TYPE,
        classes = {AuditorAwareImpl.class, AuditConfiguration.class}
))

@rashidi thanks.

@rashidi I didn't meant to say it wasn't working (that's why I put it in quote). It will obviously work if you add it yourself explicitly. What @hantsy was asking was a way for this to work out of the box (see the issue I referenced). If we did this then t's reasonable to expect @DataJpaTest to do it automatically as well, hence the duplicate.

@snicoll Yes, I hope it work seamlessly with @DataJpaTest. but the snippets provided by @rashidi is a workaround for now.

Was this page helpful?
0 / 5 - 0 ratings