junit单元测试不通过报documentationPluginsBootstrapper相关异常

Posted li150dan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了junit单元测试不通过报documentationPluginsBootstrapper相关异常相关的知识,希望对你有一定的参考价值。

这是因为Spring整合springfox-swagger2后导致的,错误信息如下:

2019-07-04 13:53:07,537 [main] [WARN] [org.springframework.context.support.GenericApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name documentationPluginsBootstrapper defined in URL [jar:file:/C:/Users/Administrator/.m2/repository/io/springfox/springfox-spring-web/2.7.0/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 6; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [javax.servlet.ServletContext]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
2019-07-04 13:53:07,540 [main] [ERROR] [org.springframework.test.context.TestContextManager] - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.Dep[email protected]] to prepare test instance [[email protected]]
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.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:47)
    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 documentationPluginsBootstrapper defined in URL [jar:file:/C:/Users/Administrator/.m2/repository/io/springfox/springfox-spring-web/2.7.0/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 6; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [javax.servlet.ServletContext]: 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)

解决办法:

测试类上面添加@WebAppConfiguration注解问题即可解决 

package com.demo.test;

import com.demo.service.MqProviderService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class) //让测试运行于Spring测试环境
@ContextConfiguration(locations = "classpath*:applicationContext.xml","classpath*:spring/spring-mybatis.xml") //Spring整合JUnit4测试时,使用注解引入多个配置文件
@WebAppConfiguration //解决整合springfox-swagger2后单元测试总是不通过的问题
public class MqTest 
    @Autowired
    private MqProviderService mqProviderService;

    @Test
    public void testQueue() 
        try 
            mqProviderService.sendDataToQueue("test_key", "hello rabbitmq");
        
        catch (Exception ex) 
            ex.printStackTrace();
        
        System.out.println("发送完毕");
    

 

以上是关于junit单元测试不通过报documentationPluginsBootstrapper相关异常的主要内容,如果未能解决你的问题,请参考以下文章

使用Junit单元测试报错“No runnable methods“

junit 单元测试报错java.lang.NoClassDefFoundError

junit单元测试成功,但是报错org.junit.runners.model.InvalidTestClassError: Invalid test class

[转]method initializationerror not found:JUnit4单元测试报错问题

JUnit 报错 method initializationerror not found:JUnit4单元测试报错问题

junit单元测试报错:java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing