Spring Boot 测试 java.lang.IllegalStateException:无法加载 ApplicationContext 并且缺少 bean

Posted

技术标签:

【中文标题】Spring Boot 测试 java.lang.IllegalStateException:无法加载 ApplicationContext 并且缺少 bean【英文标题】:Spring Boot testing java.lang.IllegalStateException: Failed to load ApplicationContext and missing bean 【发布时间】:2018-06-17 22:15:27 【问题描述】:

我似乎陷入了试图解决这个问题的循环。 我以为丢失了一个 bean,所以我将 @MockBean 它说它丢失的类的名称,再次运行它,它会说另一个类丢失了。 它出错的类甚至不是我正在测试的类。为什么会这样? 我知道测试一旦实际运行就会出错。但我可以使用它,目前我认为它还没有到达那里。

这是我的测试:

@RunWith(SpringRunner.class)
@WebMvcTest(CompanyController.class)
public class CompanyTest 

    @Autowired
    private MockMvc mvc;

    @MockBean
    CompanyService companyService;

    @Test
    public void nameChangeIsSuccessful() throws Exception 

        Company a = new Company("TestCompany",1,1);

        String oldName = a.getName();
        String newName = "Aperature Science";

        given(this.companyService.changeCompanyName("Aperature Science", 1));
    

这是错误:

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    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:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'companyController' defined in file [C:\Users\C1641195\Documents\ComercialJava\EvolveFork\Team13_fork\build\classes\main\com\nsa\evolve\controller\CompanyController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.nsa.evolve.service.PeopleTypeService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 25 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.nsa.evolve.service.PeopleTypeService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 43 more

【问题讨论】:

【参考方案1】:

没有“com.nsa.evolve.service.PeopleTypeService”类型的合格 bean

这是说在创建依赖于它的 bean 之前,您没有创建这种类型的 bean。

您是否创建了 PeopleTypeService.class 类型的 bean?

如果是这样,您可能需要设置 dependsOn 属性(或注解,具体取决于您使用的 Spring 配置设置)以确保在需要它的 bean 之前创建依赖 bean。

还有问题的 bean 有接口吗?不再严格要求,但如果不这样做可能会导致问题。

最后,您是否在 Spring 配置中的某处启用了名称匹配?如果是这样,bean 名称和 setter 和 getter 是否匹配?

【讨论】:

PeopleTypeService 确实有一个接口,它的 impl 确实有一个服务注解。不确定您对名称匹配的含义。所有代码都正常工作,只是测试不起作用。我看不到 getter 和 setter,我认为该功能不需要它们。如果我添加一个 PeopleTypeService 类型的 mockBean,它会继续,然后错误会说 bean 错误在 PeopleService,它完全是另一个类 是否在 Spring 配置的组件扫描中设置了该 bean 的包?您不能仅仅因为您设置了正在生成的服务注释就假设它正在生成,更不用说以正确的顺序生成了。您是否在配置中创建 bean? 没有。我是 Spring Boot 的新手,所以一切都是通过注释完成的,bean 是自动创建的。如果有意义的话,我自己还没有定义任何 bean。 好的,你需要为那个 bean 或者看看为什么组件扫描没有拾取它。

以上是关于Spring Boot 测试 java.lang.IllegalStateException:无法加载 ApplicationContext 并且缺少 bean的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 测试应用程序 Yaml java.lang.IllegalStateException:Tomcat 连接器处于失败状态

Spring Boot 2.1.1:java.lang.IllegalStateException:运行单元测试时无法检索@EnableAutoConfiguration 基本包错误

java.lang.IllegalStateException:在 Spring Boot 中无法加载 ApplicationContext 错误

Spring Boot 集成测试抛出错误“java.lang.IllegalStateException: Timeout on blocking read for 5000 MILLISECONDS

JPA 在 Spring Boot 中导致 java.lang.NullPointerException

SpringBoot + Mybatis + MySQL,java.lang.IllegalStateException:无法加载ApplicationContext