Spring test with @DataJpaTest can't autowire class with @Repository (but with interface repository works!)

放肆的年华 提交于 2019-11-30 20:32:58

I think I was right about the problem. After find a post on Github and read the Spring Documentation:

@DataJpaTest can be used if you want to test JPA applications. By default it will configure an in-memory embedded database, scan for @Entity classes and configure Spring Data JPA repositories. Regular @Component beans will not be loaded into the ApplicationContext.

My PersonRepository is considered a regular @Component, because it is not a Spring Data JPA repository (the interface is). So, it is not loaded.

The alternative is use @SpringBootTest instead of @DataJpaTest.

Just another alternative might be @Import as shown here https://stackoverflow.com/a/41084739/384674.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!