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

如题所述

第1个回答  2022-06-25
使用普通的@SpringBootTest进行单元测试时会将整个应用都启动,和正常启动工程没什么区别。非常耗时。

如下,启动测试。将web层也启动了。事实上根本不需要启动这个。

我们只需要启动dao就行了。在这里我们使用mybatis-spring-boot-starter-test这个依赖

测试例子

打印sql

1、java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
需要和springboot main方法放到同一级包下

2、Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

若依工程引入这个真的有点难,推荐还是使用@SpringBootTest的方式

普通单数据源请配置如下:spring.datasource.url的形式;否则单元测试启动报错'url' attribute is not specified and no embedded datasource could be configured.

有多个数据源时才去配置这样,spring.datasource.druid.master.url;
相似回答