firstly , I had better state the background here. if your project is large and has so manu dependencies in there, you just wanna your unit case to be simple and easy to start up.
in this scenario, I only load the necessary classes to test the funtion of jpa repository.
1. datasource and the datasource pool configuration should be managed by spring. I make this happen by
@ContextConfiguration(classes = {DatabaseConfiguration.class,HikariConfigurationMap.class})
2.@DataJpaTest without this annotation , exception will show up.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!
using this annotation may bring other issues, such as it would try to use an embed database ,if you do not want this happen, you can set the replace value to false by below:
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
then I also need to scan the repository and the entity so that they can be managed by spring.
@EnableJpaRepositories("com.test.jpa.repository")
@EntityScan("com.test.jpa.entity")