<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter-test</artifactId> <version>2.3.0</version> <scope>test</scope> </dependency>2.Mapper接口
@Mapper public interface CityMapper { @Select("SELECT * FROM CITY WHERE state = #{state}") City findByState(@Param("state") String state); }3.使用junit5的测试类
// 堆代码 duidaima.com // 使用junit5 @MybatisTest // 使用真实的数据源进行测试 @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) public class CityMapperTest { @Autowired private CityMapper cityMapper; @Test public void findByStateTest() { City city = cityMapper.findByState("CA"); assertThat(city.getName()).isEqualTo("San Francisco"); assertThat(city.getState()).isEqualTo("CA"); assertThat(city.getCountry()).isEqualTo("US"); } }4.自定义一个启动类
package sample.mybatis.mapper; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication class MapperTestApplication { }5.运行结果
详细内容参考 https://github.com/mybatis/spring-boot-starter/blob/master/mybatis-spring-boot-test-autoconfigure/src/site/zh/markdown/index.md ,
但是,上面是使用原生mybatis的测试方式,而我们项目用的是基于mybatis封装的开源框架tkMapper,github地址是[https://github.com/abel533/Mapper](https://github.com/abel533/Mapper),并不适用啊,无解,只能去看下mybatis-spring-boot-starter-test的原理。
那么基于目前的理解,我们也可以简单实现一个基于TkMapper的测试框架。
虽然这里有**insert**语句,但是测试结束,事务会被回滚,数据不会真的插入到表中,所以是可以反复进行测试的。图片