Creating a JPA component test

Most of our previous test examples had to start up the entire application and configure all the beans in order to execute. While that is not a big issue for our simple application, which has little code, it might prove an expensive and lengthy process for some larger, more complex enterprise-grade services. Considering that one of the key aspects of having good test coverage is a low execution time, we might want to opt out of having to bootstrap the entire application in order to test just one component, or slice, as Spring Boot refers to it.

In this recipe, we will try to create a similar test to our previous PublisherRepository one, but without starting the entire container and initializing all the beans. Conveniently, Spring Boot provides us with the @DataJpaTest annotation, which we can put on our test class, and it will automatically configure all the components necessary for the JPA functionality, but not the entire context. So beans like controllers, services, and so on, will be missing. This test is very good at quickly testing the validity of entity domain object bindings, to make sure the field names, associations, and so on, have been configured correctly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.174.76