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,
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

globalcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值