为啥我的 Bean 在 Junit 测试中没有资格成为 Autowire 候选人?

Posted

技术标签:

【中文标题】为啥我的 Bean 在 Junit 测试中没有资格成为 Autowire 候选人?【英文标题】:Why does my Bean not qualify as Autowire Candidate on Junit test?为什么我的 Bean 在 Junit 测试中没有资格成为 Autowire 候选人? 【发布时间】:2020-05-21 06:54:37 【问题描述】:

我有以下测试类,旨在在我的MVC 模型中为我的Service 类运行junit 测试。这是我的课ExpertsServiceTest.java


@RunWith(SpringRunner.class)
@RequiredArgsConstructor
@Component
public class ExpertsServiceTest 


    @Autowired
    private  ExpertRepository repository;

    @Autowired
    private ExpertsServiceImpl service;


    Experts demoExpert = new Experts(ObjectId.get(),"Steve Jobs", "Enterpreneur",
            Availability.BUSY, Language.CHINESE);



    @Before
    public void setUp() throws Exception
        service.deleteAll();
        service.createExpert(demoExpert);

    

    @After
    public void tearDown() throws Exception
        service.deleteAll();
    

    @Test
    public void testFindAll()
        List<Experts> expertEntries = service.findAll();
        assertEquals(2, expertEntries.size());
    

但是当我运行测试时,我遇到了以下错误:


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demo.ExpertsServiceTest': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'demo.ExpertRepository' 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:393)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'demo.ExpertRepository' 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.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
    ... 27 more

有谁明白为什么会发生这种情况以及我该如何解决它?提前感谢您的帮助

【问题讨论】:

No found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:的可能重复 集成测试用例需要@SpringBootTest注解 【参考方案1】:

您没有提供应用程序上下文信息

请用@ContextConfiguration注解测试类并提供一个配置类。内容如下

package com.example;

@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from the OrderServiceConfig class
@ContextConfiguration(classes=OrderServiceConfig.class, loader=AnnotationConfigContextLoader.class)
public class OrderServiceTest 

    @Autowired
    private OrderService orderService;

    @Test
    public void testOrderService() 
        // test the orderService
    

Source

【讨论】:

【参考方案2】:

你没有spring上下文,你应该使用:'@SpringBootTest'注解。 你可以在这里阅读更多:SpringRunner vs SpringBootTest

【讨论】:

以上是关于为啥我的 Bean 在 Junit 测试中没有资格成为 Autowire 候选人?的主要内容,如果未能解决你的问题,请参考以下文章

在Camel JUnit中自动装配Spring Bean

在Junit测试中没有类型为Error的限定bean

在 Spring Boot 应用程序的 JUnit 测试中,自动装配的 JPA 存储库没有合格的 bean

如何在用 Kotlin 编写的 JUnit 5 测试类中注入 Spring bean?

Spring 在 JUnit 测试中找不到 DataSource Bean

使用 Spring Boot 2.1.15.RELEASE(junit 4) 进行集成测试的 Bean 自动装配为 null