I’ve seen 2 youtube videos on Spring Boot testing: one by Josh Long and the other by S. Jalukar and M. Bhave (software engineers from Pivotal).
I found something in their RepositoryTest which looks like an inconsistency to me.
The female developers, while testing the repository’s custom findByName() method do this:
Car savedCar = entityManager.persistFlushFind(new Car(”prius”,);
Car car = repository.findByName(”prius”);
assertThat(car.getName()).isEqualTo(savedCar.getName());
And they argue that it’s important ot use TestEntityManager’s persistFlushFind(entity) instead of repository.save(entity) as the former saves directly to the database and the latter saves only to the cache, so we’ll not be actually testing what reallly happens in a real application.
In Josh’s RepositoryTest, he does the following:
repository.save(new Reservation(null, ”Jane”);
Collection byReservationName = repository.findByReservationName(”Jane”);
assertThat(byReservationName.iterator().next().getReservatonName()).isEqualTo(”Jane”);
BTW. Josh also has a separate JPATest in which he uses TestEntityManager:
Reservation jane = persistFlushFind(new Reservation(null, ”Jane”);
assertThat(jane.getReservationName()).isEqualTo(”Jane”);
They both use @DataJpaTest , which itself is annotated with @AutoConfigureCache with @PropertyMapping("spring.cache.type") CacheType cacheProvider() default CacheType.NONE;
So, is there really caching when using repository.save(entity) in a test? Who’s testing the repository correctly? Are the two women actually doing in one test what Josh does in two? Please clarify.