51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 552|回复: 2
打印 上一主题 下一主题

SpringBoot单元测试时,无法创建bean:Error creating bean with name 'Bean'

[复制链接]
  • TA的每日心情
    无聊
    昨天 09:47
  • 签到天数: 528 天

    连续签到: 1 天

    [LV.9]测试副司令

    跳转到指定楼层
    1#
    发表于 2021-12-6 09:52:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    1测试积点

    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
    复制代码


    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    13 小时前
  • 签到天数: 1803 天

    连续签到: 2 天

    [LV.Master]测试大本营

    2#
    发表于 2021-12-7 09:44:49 | 只看该作者
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    12 小时前
  • 签到天数: 1518 天

    连续签到: 2 天

    [LV.Master]测试大本营

    3#
    发表于 2021-12-7 10:30:50 | 只看该作者
    检查下环境配置
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-19 22:41 , Processed in 0.060261 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表