java 使用@DataJpaTest 的 Spring 测试无法使用 @Repository 自动装配类(但接口存储库可以工作!)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48347088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:02:31  来源:igfitidea点击:

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

javaspringspring-data-jpaintegration-testingspring-test

提问by Dherik

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

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

First, the error:

首先,错误:

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() {
    }
}

The repository class:

存储库类:

@Repository
public class PersonRepository {
    //code
}

The repository interface:

存储库界面:

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

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 @DataJpaTestdoes not have support for class repositories.

遇到相同错误糟糕体验后,我试图在我的配置中找到一些细节或测试导致此问题的原因。另一种可能性是@DataJpaTest不支持类存储库。

回答by Dherik

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

我认为我对这个问题是正确的。在Github 上找到帖子并阅读Spring 文档后

@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.

如果要测试 JPA 应用程序,可以使用 @DataJpaTest。默认情况下,它将配置一个内存中的嵌入式数据库,扫描 @Entity 类并配置 Spring Data JPA 存储库。常规的@Component bean 不会加载到 ApplicationContext 中。

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

MyPersonRepository被认为是常规的@Component,因为它不是 Spring Data JPA 存储库(接口是)。所以,它没有加载。

The alternative solution is to use @SpringBootTestinstead of @DataJpaTest.

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

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

此解决方案的缺点是将在运行测试时加载所有上下文,并因此禁用测试切片。但是做这份工作。

Another option still using @DataJpaTestis include a @Repositoryfilter annotation, like this:

另一个仍在使用的选项@DataJpaTest是包含@Repository过滤器注释,如下所示:

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

回答by Betlista

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

另一种选择可能@Import如下所示https://stackoverflow.com/a/41084739/384674