SpringBoot如何只对mapper的sql进行测试

2024-07-17 15:59:22 浏览数 (3)

SpringBoot如何只对mapper的sql进行测试

一、介绍

在开发SpringBoot项目的时候,使用Mybatis进行DAO层的开发,往往在写了一堆mapper.java后进行测试

那么能不能就单单仅对mapper.java进行测试呢?

不用加载全部的bean,仅仅把数据源,SqlSessionmapper等相关的bean创建出来

弄成一个简易的SpringBoot容器,如此一来,测试的速度就大大加快了

二、代码

1)前置代码

首先,是我们的实体类

代码语言:javascript复制
package com.banmoon.entity;
​
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
​
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@TableName("tb_power_mockito")
public class PowerMockitoEntity {
​
    @TableId
    private Integer id;
​
    private String value;
​
    private Boolean status;
​
}

再然后,是我们的PowerMockitoMapper.java

代码语言:javascript复制
package com.banmoon.mapper;
​
import com.banmoon.entity.PowerMockitoEntity;
import org.apache.ibatis.annotations.Mapper;
​
@Mapper
public interface PowerMockitoMapper {
​
    int insert(PowerMockitoEntity entity);
​
    int updateById(PowerMockitoEntity entity);
​
    Long countByValue(String value);
}

以及PowerMockitoMapper.xml

代码语言:javascript复制
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.banmoon.mapper.PowerMockitoMapper">
    <insert id="insert" parameterType="com.banmoon.entity.PowerMockitoEntity">
        insert into tb_power_mockito (`status`, `value`)
        values (#{status}, #{value})
    </insert>
​
    <update id="updateById" parameterType="com.banmoon.entity.PowerMockitoEntity">
        update tb_power_mockito
        <set>
            <if test="status != null">
                `status` = #{status},
            </if>
            <if test="value != null">
                `value` = #{value},
            </if>
        </set>
        where id = #{id}
    </update>
​
    <select id="countByValue" resultType="java.lang.Long" parameterType="java.lang.String">
        select count(*) from tb_power_mockito where `value` = #{value}
    </select>
</mapper>

2)测试代码

代码语言:javascript复制
package com.banmoon.mapper;
​
import cn.hutool.core.util.RandomUtil;
import com.banmoon.entity.PowerMockitoEntity;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
​
import javax.annotation.Resource;
import javax.sql.DataSource;
​
/**
 * @author banmoon
 * @date 2024/07/16 11:36:41
 */
@SpringBootTest(classes = {
        PowerMockitoMapper.class,
        DataSourceAutoConfiguration.class,
        MybatisPlusAutoConfiguration.class,
        DataSource.class,
        SqlSessionFactory.class
})
@RunWith(SpringRunner.class)
@MapperScan("com.banmoon.mapper")
public class PowerMockitoMapperTest {
​
    @Resource
    private PowerMockitoMapper powerMockitoMapper;
​
    @Test
    public void testInsert() {
        PowerMockitoEntity entity = new PowerMockitoEntity();
        entity.setValue(RandomUtil.randomString(8));
        entity.setStatus(Boolean.TRUE);
        int i = powerMockitoMapper.insert(entity);
        Assert.assertEquals(1, i);
    }
​
    @Test
    public void testUpdateById() {
        PowerMockitoEntity entity = new PowerMockitoEntity();
        entity.setId(0);
        entity.setValue(RandomUtil.randomString(8));
        entity.setStatus(Boolean.TRUE);
        int i = powerMockitoMapper.updateById(entity);
        Assert.assertEquals(0, i);
    }
​
    @Test
    public void testCountByValue() {
        Long i = powerMockitoMapper.countByValue("banmoon");
        Assert.assertEquals(new Long(0), i);
    }
​
}

在上面的代码中,使用了@SpringBootTest注解,并指定了需要进行初始化的bean,如此一来就能达到我们的效果

三、最后

实际上,我在运行上面代码的时候,遇到了一个问题。

那就是我使用了Mybatis-plus中的mpw数据库账号密码加密的方式,导致了数据库连接失败。

这就很尴尬了,得想想用其他办法去解决。

0 人点赞