使用@DataJpaTest 的 Spring 测试无法使用 @Repository 自动装配类(但接口存储库可以工作!) [英] Spring test with @DataJpaTest can't autowire class with @Repository (but with interface repository works!)

查看:49
本文介绍了使用@DataJpaTest 的 Spring 测试无法使用 @Repository 自动装配类(但接口存储库可以工作!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么我不能自动装配类存储库,但我可以为相同测试自动装配相同包中的接口存储库.当我启动应用程序时,相同的存储库按预期工作.

I'm trying to understand why I can't autowire a class repository but I can autowire a interface repository in the same package for the same test. The same repository works as expected when I start the application.

一、错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.app.person.repository.PersonRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultPersonbleBeanFactory.raiseNoMatchingBeanFound(DefaultPersonbleBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultPersonbleBeanFactory.doResolveDependency(DefaultPersonbleBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultPersonbleBeanFactory.resolveDependency(DefaultPersonbleBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 28 more

我有一个非常简单的例子.测试:

I have a very simple example. The test:

@RunWith(SpringRunner.class)
@DataJpaTest
public class PersonRepositoryTest {

    @Autowired
    private PersonRepository personRepository; // fail...

    @Autowired
    private PersonCrudRepository personCrudRepository; // works!

    @Test
    public void findOne() {
    }
}

存储库类:

@Repository
public class PersonRepository {
    //code
}

存储库接口:

@Repository
public interface PersonCrudRepository extends CrudRepository<Person, Long> {
}

遇到同样错误的糟糕体验之后,我试图在我的配置中找到一些细节或测试什么负责这个问题.另一种可能性是 @DataJpaTest 不支持类存储库.

After a bad experience with this same error, I'm trying to find some detail in my configuration or test what is responsible for this problem. Another possibility is the @DataJpaTest does not have support for class repositories.

推荐答案

我认为我对这个问题的看法是正确的.找到Github上的帖子并阅读Spring 文档:

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

@DataJpaTest 可以用于测试 JPA 应用程序.经过默认它会配置一个内存中的嵌入式数据库,扫描@Entity 类并配置 Spring Data JPA 存储库.常规的@Component bean 不会加载到 ApplicationContext 中.

@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 被认为是常规的 @Component,因为它不是 Spring Data JPA 存储库(接口是).所以,它没有加载.

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

另一种解决方案是使用 @SpringBootTest 而不是 @DataJpaTest.

The alternative solution is to use @SpringBootTest instead of @DataJpaTest.

此解决方案的缺点是会在运行测试时加载所有您的上下文,并因此禁用测试切片.但要做好工作.

The disadvantage with this solution is that will load all your context while running your test and, with this, disabling the test slicing. But do the job.

另一个仍然使用 @DataJpaTest 的选项是包含一个 @Repository 过滤器注解,像这样:

Another option, still using @DataJpaTest, is include a @Repository filter annotation, like this:

@DataJpaTest(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Repository.class))

这篇关于使用@DataJpaTest 的 Spring 测试无法使用 @Repository 自动装配类(但接口存储库可以工作!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆