Closed hantsy closed 6 years ago
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.
Any workaround for @EnableSpannerAuditing and @SpringBootTest?
Faced with the same problem in the development and the solutions above I did not like - they are not obvious, they are incomprehensible. There should be a simple way to solve such problems. It turns out there is such a solution. Let's go what each annotation does.
@DataJpaTest - raises Mock Entity Manager and adds all Bеаns like Repository in context.
@SpringBootTest - on startup it starts looking for the @SpringBootConfiguration annotation, since it's not explicitly written anywhere, it gets to @SpringBootApplication, it finds it there, then Spring thinks it found the configuration and assigns to use @SpringBootApplication to initialize the context, and ends up lifting the whole context. In this case, everything works because all the Bеаns get into the context, but the bad thing is that the entire context rises and we needed only one or more services.
To solve this problem we need to create a configuration for the test and mark with a special annotation @TestConfiguration, after that we enable @EnableJpaAuditing in this configuration. Now we have the configuration ready, all we have to do is initialize the context with this configuration as follows @ContextConfiguration(classes = JpaConfig.class).
In this way you can include different auditors and not only, regardless of the type of.
@DataJpaTest
@ContextConfiguration(classes = DefaultJpaTestConfiguration.class)
class UserRepositoryTest {
@Autowired
private TestEntityManager entityManager;
...
}
@TestConfiguration
@EnableJpaAuditing
public class DefaultJpaTestConfiguration {
}
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