I have this sprig boot ( version 1.5.6) application that uses the following:
- spring boot starter data jpa (jpa repositories and entities)
- spring boot starter data gemfire (no repository or entities)
- spring boot starter data rest (for HAL support)
Now, I am creating the unit tests for this application. In one test case I have following annotation:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.cloud.enabled=false" })
The test initializes the jpa repository correctly and I am able to test the same.
Then I have another test with following annotation:
@RunWith(SpringRunner.class)
@WebMvcTest(MyRestController.class)
This test sets up the Mockmvc but it does not initializes the JPA repositories. It only initialize the MVC part of configuration. But I need the JPA repositories to be initialized as well. I have test data setup with data.sql file which is loaded as in-memory H2 database. The error I am getting is:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
I have tried multiple things which have not worked out:
- Using both annotations does not work as the class can have only one "BootstrapWith" annotation.
- @EnableJpaRepositories does not work
- @AutoConfigureTestDatabase does not work
I do see the following while the context initialization:
.s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
Now since spring is able to autowire the jpa repository in the first test and it works fine in the application, I think it should be able to autowire the repository in webMvc test case as well.
I could create a configuration file and initialize the entity manager, data source, etc in the test package but if there is a way to autowire things with spring, then I don't want to manage that configuration.
Please suggest.