51Testing软件测试论坛

标题: SpringBoot单元测试时,无法创建bean:Error creating bean with name 'Bean' [打印本页]

作者: 测试积点老人    时间: 2021-12-6 09:52
标题: SpringBoot单元测试时,无法创建bean:Error creating bean with name 'Bean'

springboot项目,通过http访问controller接口一切正常,但是进行单元测试时,无法创建bean

1、项目依赖:

  1. <!--JWT依赖-->
  2.         <dependency>
  3.             <groupId>io.jsonwebtoken</groupId>
  4.             <artifactId>jjwt-api</artifactId>
  5.             <version>0.10.5</version>
  6.         </dependency>
  7.         <dependency>
  8.             <groupId>io.jsonwebtoken</groupId>
  9.             <artifactId>jjwt-impl</artifactId>
  10.             <version>0.10.5</version>
  11.         </dependency>
  12.         <dependency>
  13.             <groupId>io.jsonwebtoken</groupId>
  14.             <artifactId>jjwt-jackson</artifactId>
  15.             <version>0.10.5</version>
  16.         </dependency>
  17.         <!--nacos服务注册与发现-->
  18.         <dependency>
  19.             <groupId>com.alibaba.cloud</groupId>
  20.             <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  21.             <version>2.2.3.RELEASE</version>
  22.         </dependency>
  23.         <!--spring web服务依赖-->
  24.         <dependency>
  25.             <groupId>org.springframework.boot</groupId>
  26.             <artifactId>spring-boot-starter-web</artifactId>
  27.         </dependency>
  28.         <!--安全检查依赖-->
  29.         <dependency>
  30.             <groupId>org.springframework.boot</groupId>
  31.             <artifactId>spring-boot-starter-actuator</artifactId>
  32.         </dependency>
  33.         <!--mysql依赖-->
  34.         <dependency>
  35.             <groupId>mysql</groupId>
  36.             <artifactId>mysql-connector-java</artifactId>
  37.             <version>8.0.15</version>
  38.         </dependency>
  39.         <!--mybatis依赖-->
  40.         <dependency>
  41.             <groupId>org.mybatis.spring.boot</groupId>
  42.             <artifactId>mybatis-spring-boot-starter</artifactId>
  43.             <version>2.1.4</version>
  44.         </dependency>
  45.         <!--alibaba druid连接池依赖-->
  46.         <dependency>
  47.             <groupId>com.alibaba</groupId>
  48.             <artifactId>druid-spring-boot-starter</artifactId>
  49.             <version>1.1.17</version>
  50.         </dependency>
  51.         <!--springboot测试依赖-->
  52.         <dependency>
  53.             <groupId>org.springframework.boot</groupId>
  54.             <artifactId>spring-boot-starter-test</artifactId>
  55.             <scope>test</scope>
  56.         </dependency>
  57.         <!--fastjson依赖-->
  58.         <dependency>
  59.             <groupId>com.alibaba</groupId>
  60.             <artifactId>fastjson</artifactId>
  61.             <version>1.2.75</version>
  62.         </dependency>
  63.         <!--工具类依赖-->
  64.         <dependency>
  65.             <groupId>org.projectlombok</groupId>
  66.             <artifactId>lombok</artifactId>
  67.             <version>1.18.20</version>
  68.         </dependency>
  69.         <dependency>
  70.             <groupId>org.apache.commons</groupId>
  71.             <artifactId>commons-lang3</artifactId>
  72.             <version>3.11</version>
  73.         </dependency>
  74.         <dependency>
  75.             <groupId>org.apache.commons</groupId>
  76.             <artifactId>commons-collections4</artifactId>
  77.             <version>4.4</version>
  78.         </dependency>
  79.         <dependency>
  80.             <groupId>cn.hutool</groupId>
  81.             <artifactId>hutool-all</artifactId>
  82.             <version>5.7.6</version>
  83.         </dependency>
复制代码

2、配置文件

  1. server:
  2.   port: xxxx
  3.   servlet:
  4.     context-path: /authority
  5. spring:
  6.   application:
  7.     name: springboot-authority
  8.   datasource:
  9.     url: jdbc:mysql://47.98.xxx.xx:3306/springcloud?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&nullCatalogMeansCurrent=true
  10.     username: xxxx
  11.     password: xxxx
  12.     driver-class-name: com.mysql.cj.jdbc.Driver
  13.   cloud:
  14.     nacos:
  15.       discovery:
  16.         group: springcloud
  17.         enabled: true
  18.         server-addr: 47.xxx.xxx:xxx
  19.         namespace: 25e57f0a-b543-xxx-xxx-xxxx
  20.         metadata:
  21.           management:
  22.             context-path: ${server.servlet.context-path}/actuator
  23. management:
  24.   endpoints:
  25.     web:
  26.       exposure:
  27.         include: '*'
  28.   endpoint:
  29.     health:
  30.       show-details: always
  31. mybatis:
  32.   configuration:
  33.     map-underscore-to-camel-case: true
  34.   mapper-locations: classpath:mapper/*.xml
复制代码

3、启动类

  1.   @EnableDiscoveryClient
  2.   @SpringBootApplication
  3.   @MapperScan("zss.springcloud.dao")
  4.   public class AuthorityApplication {
  5.       public static void main(String[] args) {
  6.           SpringApplication.run(AuthorityApplication.class, args);
  7.       }
  8.   }
  9. ```java



  10. 4、junit测试类
  11. ```java
  12. @SpringBootTest(classes = AuthorityApplication.class)
  13. @RunWith(SpringRunner.class)
  14. public class MyTest {
  15.     @Resource
  16.     private SpringApplication application;
  17.     @Test
  18.     public void test01(){
  19.         System.out.println(123);
  20.     }
  21. }
复制代码

4、报错信息

  1. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'zss.springcloud.MyTest': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.SpringApplication' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
  2.     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:321)
  3.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420)
  4.     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:392)
  5.     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:119)
  6.     at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
  7.     at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
  8.     at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
  9.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
  10.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
  11.     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  12.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
  13.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
  14.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
  15.     at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
  16.     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
  17.     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
  18.     at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
  19.     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
  20.     at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  21.     at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  22.     at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
  23.     at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
  24.     at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
  25.     at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  26.     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
  27.     at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
  28.     at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
  29.     at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
  30.     at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
  31.     at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
  32. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.SpringApplication' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
  33.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717)
  34.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273)
  35.     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
  36.     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:521)
  37.     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:497)
  38.     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:650)
  39.     at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:239)
  40.     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
  41.     at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessProperties(CommonAnnotationBeanPostProcessor.java:318)
  42.     ... 29 more
复制代码



作者: 海海豚    时间: 2021-12-7 09:44
https://blog.csdn.net/qq_34761108/article/details/88416898  看下这个
作者: qqq911    时间: 2021-12-7 10:30
检查下环境配置




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2