4

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.

enhancedJack
  • 265
  • 1
  • 6
  • 15
  • 1
    I've also been wondering this. I haven't found much material that says this is the case one way or the other. Based on this: https://www.youtube.com/watch?v=QjaoAWLlGGs (around 28:00 onwards), Phil Webb uses an entityManager to persist the entity to the database and then uses the repo to retrieve the entity. He doesn't explain the use of the entity manager but based on the talk with M.Bhave and S.Jalukar, it looks like hes taking the same approach to circumvent the cache. Only other way to check is to deep dive into the source(which i plan to do at some stage) – jbailie1991 Jul 05 '18 at 10:40
  • 1
    Might have found something. See answer 2 here:https://stackoverflow.com/questions/38893831/spring-data-crudrepositorys-save-method-and-update based on that, on save it will look in the level 2 cache for the entity first, and if not found, retrieve it from the database – jbailie1991 Jul 05 '18 at 10:53

0 Answers0