junit单元测试 mybatis 单独测试 springboot

这篇具有很好参考价值的文章主要介绍了junit单元测试 mybatis 单独测试 springboot。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

省流:

正文

一、直接测(无需配置扫描和xml)

1. 场景

2. 无需配置扫描,直接在测试类注入Mapper

3. 报错

补充:

关于@RunWith

常见的报错:

1.包名不同导致报错


省流:

test目录下配置文件:application.yml中配置数据库信息。

test目录下各类:直接写测试类即可,直接调用main目录下各Mapper类和实体类即可。

测试类: 

@RunWith(SpringRunner.class)
@MybatisPlusTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(false)
public class Test1 {

    @Autowired
    private T1Mapper t1Mapper;


    @Test
    public void t1() {
        t1Mapper.selectOne();
    }

}

application.yml配置文件: 

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    # 生产
    url: jdbc:mysql://ip:port/xxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: root
    # 初始连接数
    initialSize: 5
    # 最小连接池数量
    minIdle: 10
    # 最大连接池数量
    maxActive: 20
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    # 配置一个连接在池中最大生存的时间,单位是毫秒
    maxEvictableIdleTimeMillis: 900000
    # 配置检测连接是否有效
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    webStatFilter:
      enabled: true
    statViewServlet:
      enabled: true
      # 设置白名单,不填则允许所有访问
      allow:
      url-pattern: /druid/*
      # 控制台管理用户名和密码
      login-username:
      login-password:
    filter:
      stat:
        enabled: true
        # 慢SQL记录
        log-slow-sql: true
        slow-sql-millis: 1000
        merge-sql: true
      wall:
        config:
          multi-statement-allow: true
mybatis-plus:
#  mapper-locations: classpath:/mapper/*Mapper.xml
#  typeAliasesPackage: com.ali.entity
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

目录结构:

目录结构

src
--main
--test
  -- java
     -- package和java文件
  -- resources
     -- application.yml

正文

一、直接测(无需配置扫描和xml)

1. 场景

在公司项目里,有时候想测下某条sql(不想调整个接口),有时想直接调用测试环境的数据库,或者生产环境的数据库。就只需单独使用mybatis测,无需启动整个springboot。

2. 无需配置扫描,直接在测试类注入Mapper

无需配置扫描等。

无需如下配置:

启动类加注解:@MapperScan("com.cloud.xxx.mapper")
配置文件:mybatis-plus.mapper-locations=classpath:com/cloud/xxx/mapper/xml/*.xml

注意:

直接调用mybatis,如果test目录中存在别人写的其他测试类,尤其是有些测试类用springboot启动的,会影响到你测试类启动,可能会报如下错误。你不需要去解决这些错,而是直接把其他人的测试类干掉,你的测试类就能启动成功了

3. 报错

报错1: 

No qualifying bean of type 'com.xxx.mapper.xxxMapper' available: 
expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

 这个报错一般是指没有Mapper,通常是没有扫描到,需要加 @MapperScan

报错2: 

Annotation-specified bean name 'xxxMapper' for bean class conflicts with existing,

 这个报错是存在多个相同名字的Mapper。

补充:

关于@RunWith

@RunWith:就是一个运行器
@RunWith(JUnit4.class) 指用JUnit4测试工具来运行测试。
@RunWith(SpringJUnit4ClassRunner.class):指让类运行在Spring的测试环境,以便测试开始时自动创建Spring应用上下文,并使用JUnit4测试工具运行测试。
@RunWith(SpringRunner.class):SpringRunner继承了 SpringJUnit4ClassRunner ,所以等价于@RunWith(SpringJUnit4ClassRunner.class),为了名字简短,方便使用
@RunWith(Suite.class):一套测试集合(suite指一套,使用RunWith测试套件)

通常,使用 @RunWith(SpringRunner.class)

常见的报错:

1.包名不同导致报错

报错 Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration (SpringBoot测试)_globalcoding的博客-CSDN博客

参考

Spring boot Mybatis-Plus数据库单测实战(三种方式)_@mybatisplustest_CuteXiaoKe的博客-CSDN博客

在 springboot 中进行单独的 mybatis 单元测试 - 简书

MyBatis plus和maven的依赖_mybatisplus maven依赖_阿飞0x6c717a的博客-CSDN博客

测试类的@RunWith与@SpringBootTest注解_Morning sunshine的博客-CSDN博客

======================分割线======================= 

文章到此已经结束,以下是紫薯布丁

@RunWith(SpringRunner.class)
@MybatisPlusTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Rollback(false)
public class Test1 {

    @Autowired
    private T1Mapper t1Mapper;


    @Test
    public void t1() {
        t1Mapper.selectOne();
    }

}

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    # 生产
    url: jdbc:mysql://ip:port/xxx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: root
    # 初始连接数
    initialSize: 5
    # 最小连接池数量
    minIdle: 10
    # 最大连接池数量
    maxActive: 20
    # 配置获取连接等待超时的时间
    maxWait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    timeBetweenEvictionRunsMillis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    minEvictableIdleTimeMillis: 300000
    # 配置一个连接在池中最大生存的时间,单位是毫秒
    maxEvictableIdleTimeMillis: 900000
    # 配置检测连接是否有效
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    webStatFilter:
      enabled: true
    statViewServlet:
      enabled: true
      # 设置白名单,不填则允许所有访问
      allow:
      url-pattern: /druid/*
      # 控制台管理用户名和密码
      login-username:
      login-password:
    filter:
      stat:
        enabled: true
        # 慢SQL记录
        log-slow-sql: true
        slow-sql-millis: 1000
        merge-sql: true
      wall:
        config:
          multi-statement-allow: true
mybatis-plus:
#  mapper-locations: classpath:/mapper/*Mapper.xml
#  typeAliasesPackage: com.ali.entity
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

test目录结构

src

--main

--test

  -- java

     -- package和java文件

  -- resources

     -- application.yml

No qualifying bean of type 'com.xxx.mapper.xxxMapper' available: 
expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


Annotation-specified bean name 'xxxMapper' for bean class conflicts with existing,
 文章来源地址https://www.toymoban.com/news/detail-469570.html

到了这里,关于junit单元测试 mybatis 单独测试 springboot的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • springboot Junit单元测试默认事务不提交

    因为以前总觉得Junit单元测试配置比较繁琐,代码功能大多使用main方法或者postman测试,直到最近才使用单元测试,在测试过程中遇到了事务不提交的问题,一直以为是代码问题,后来才直到单元测试 默认不提交事务 ,记录下来,防止以后再次踩坑。 如上,入库操作不会实现

    2024年02月10日
    浏览(11)
  • SpringBoot 中如何利用 Junit 实现单元测试?

    SpringBoot 中如何利用 Junit 实现单元测试?

    2024软件测试面试刷题,这个小程序(永久刷题),靠它快速找到工作了!(刷题APP的天花板)_软件测试刷题小程序-CSDN博客 文章浏览阅读2.5k次,点赞85次,收藏11次。你知不知道有这么一个软件测试面试的刷题小程序。里面包含了面试常问的软件测试基础题,web自动化测试、

    2024年03月11日
    浏览(12)
  • SpringBoot2---单元测试(Junit5)(1)

    SpringBoot2---单元测试(Junit5)(1)

    org.junit.vintage junit-vintage-engine test org.hamcrest hamcrest-core org.springframework.boot spring-boot-starter-test test 现在版本: @SpringBootTest class Boot05WebAdminApplicationTests { @Test void contextLoads() { } } 以前: @SpringBootTest + @RunWith(SpringRunner.class) SpringBoot整合Junit以后。 编写测试方法:@Test标注(注意需要

    2024年04月29日
    浏览(9)
  • 静态方法 单元测试 springboot+mokito+junit5

    CodecUtils的方法是静态类,使用@InjectMocks不能有用,因为这个注解只能用于非静态的对象。 想要为静态方法写单元测试,可以使用Mockito.mockStatic(Class classToMock)方法,它可以返回一个MockedStatic对象,用于模拟静态方法的调用。 1.导入依赖 2.单元测试 可以参考如下地址,了解如何

    2024年04月25日
    浏览(8)
  • SpringBoot单元测试--Mockito+Junit5框架使用

    SpringBoot单元测试--Mockito+Junit5框架使用

    作为程序员为了提前发现代码bug,优化代码; 通常我们写完某个功能模块代码后都需要写单元测试对代码块进行测试(特别是敏捷开发中);Java项目最常用的单元测试框架即为Junit(目前最新版本为Junit5),SpringBoot本身也整合了该框架。在写单元测试时代码块中的调到第三方接口方

    2024年02月02日
    浏览(12)
  • junit单元测试mock常用写法-springboot与springmvc

    做单元测试时候,经常会mock一些方法,得到期望的返回值,这里记录一下常见的public、private、static方法,以及设计到的内部调用的方法 这里采用maven,引入powermock即可 所有单元测试的测试类都继成BaseTest即可 (1)springboot的示例 (2)springmvc的示例 有如下类需要编写单元测

    2024年02月02日
    浏览(10)
  • SpringBoot 实战:JUnit5+MockMvc+Mockito 做好单元测试

    SpringBoot 实战:JUnit5+MockMvc+Mockito 做好单元测试

    因为继承了 spring-boot-starter-parent ,所以我们依赖的 spring-boot-starter-test 不需要写具体的版本,可以直接集成父级的版本定义。其中, spring-boot-starter-web 是用于提供 REST API 的 web 容器, spring-boot-starter-test 可以提供各种测试框架的, spring-boot-maven-plugin 是将 SpringBoot 应用打包为

    2024年04月15日
    浏览(11)
  • 【SpringBoot】org.junit.runners.model.InvalidTestClassError 单元测试类报错(已解决)

    出现该报错是因为 @Test 注解的错误使用。 @Test 注解的正确使用 : 权限必须是public 不能有参数 返回值类型是void 本类的其他的使用了Test注解的方法返回值也必须是void 正确导包 import org.junit.Test 一般正确使用 @Test 下,就没有报错了。 但是,我们可以继续深入探讨。 SpringBo

    2024年02月01日
    浏览(11)
  • springboot项目使用Junit5 + mockito + jacoco 实现单元测试以及代码覆盖率检查

    在创建springboot项目时会默认添加spring-boot-starter-test依赖,其中已经包含了junit、mockito依赖,根据springboot版本的不同junit和mockito的版本也会有所不同 先说一下各自功能: junit只说一点,junt4和junit5的注解不同,使用方式略有差异,其他不赘述了,基本用法都懂。 mockito是mock的

    2023年04月23日
    浏览(16)
  • 【 SpringBoot单元测试 和 Mybatis 增,删,改 操作 】

    【 SpringBoot单元测试 和 Mybatis 增,删,改 操作 】

    单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。 在Java中单元测试的最小单元是类 单元测试是开发者编写的一小段代码,用于检验被测代码的一个很小的、很明确的功能是否正确。执行单元测试,就是为了证明这段代码的行为和我们期望是否一致

    2023年04月23日
    浏览(10)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包