I have a unit test that includes a couple of simple operations with a database. Interaction with database is implemented with Spring Data and JPA. My environment is Spring Boot 1.4.
My test looks like:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
@SpringBootTest(classes = {Application.class})
@Transactional
public class OrderRepositoryTest {
@Autowired
private OrderRepository orderRepository;
@Autowired
EntityManager entityManager;
@Test
public void can_create_and_find_one_order() {
String orderId = "XX_YY";
Order order1 = createAnOrder();
orderRepository.save(order1);
entityManager.flush();
entityManager.clear();
Order order2 = orderRepository.findOne(orderId);
Assertions.assertThat(order2).isEqualTo(order1);
}
}
Where Application is the Spring Boot entry point, and contains the EnableAutoConfiguration annotation:
@EnableAutoConfiguration
@EnableConfigurationProperties
@ComponentScan(basePackageClasses = {
WebMvcConfig.class,
ServicesConfiguration.class,
WebSecurityConfiguration.class
})
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
return applicationBuilder.sources(Application .class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The test does load the JPA layer, and performs the operation. But it also loads the MVC configuration, the security configuration, and plenty of other layers that I'm not testing.
Question: How could I replace the EnableAutoConfiguration and load only the JPA layer that I need?
Ideally, it would a configuration class similar to extending ´WebMvcConfigurerAdapter´ in a way that I can use it from the unit test and the application entry point, so my unit test also validates the configuration:
@Configuration
@EnableJpaRepositories(basePackages = "class.path.to.my.repositories")
public class PersistenceConfiguration {
// Whatever needs to be here
// ...
}