spring boot單元測試之九:用@MybatisTest注解基於mysql+mybatis測試mapper/sql(spring boot 2.4.4)


一,演示項目的相關信息

1,地址:

https://github.com/liuhongdi/mybatistest

2,功能:演示了基於mysql數據庫做sql測試

3,項目結構:如圖:

 

 

說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest

         對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/

說明:作者:劉宏締 郵箱: 371125307@qq.com

二,配置文件說明:

1,pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--mybatis begin-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter-test</artifactId>
            <version>2.1.3</version>
            <scope>test</scope>
        </dependency>

        <!--mybatis end-->

        <!--mysql begin-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--mysql end-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2, application-dev.yml

#error
server:
  error:
    include-stacktrace: always
#errorlog
logging:
  level:
    org.springframework.web: trace
#mysql
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/store?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: lhddemo
    driver-class-name: com.mysql.cj.jdbc.Driver
    maximum-pool-size: 12
    minimum-idle: 10
    idle-timeout: 500000
    max-lifetime: 540000
#mybatis
mybatis:
  mapper-locations: classpath:/mapper/*Mapper.xml
  type-aliases-package: com.example.demo.mapper
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

三,java代碼說明

1,pojo/Goods.java

public class Goods {
    //商品id
    Long goodsId;
    public Long getGoodsId() {
        return this.goodsId;
    }
    public void setGoodsId(Long goodsId) {
        this.goodsId = goodsId;
    }

    //商品名稱
    private String goodsName;
    public String getGoodsName() {
        return this.goodsName;
    }
    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    //商品標題
    private String subject;
    public String getSubject() {
        return this.subject;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }

    //商品價格
    private BigDecimal price;
    public BigDecimal getPrice() {
        return this.price;
    }
    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    //庫存
    int stock;
    public int getStock() {
        return this.stock;
    }
    public void setStock(int stock) {
        this.stock = stock;
    }

    @Override
    public String toString(){
        return " Goods:goodsId=" + goodsId +" goodsName=" + goodsName+" subject=" + subject+" price=" + price+" stock=" + stock;
    }
}

2,mapper/GoodsMapper.java

@Repository
@Mapper
public interface GoodsMapper {
    Goods selectOneGoods(Long goodsId);
    int insertOneGoods(Goods goods);
    int updateOneGoods(Goods goods);
}

3,mapper/GoodsMapper.xml

<?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.mybatistest.demo.mapper.GoodsMapper">

    <select id="selectOneGoods" parameterType="long" resultType="com.mybatistest.demo.pojo.Goods">
        select * from goods where goodsId=#{goodsId}
    </select>

    <update id="updateGoodsStock">
        UPDATE goods SET
        stock = stock+#{changeAmount,jdbcType=INTEGER}
        WHERE goodsId = #{goodsId,jdbcType=BIGINT}
    </update>

    <insert id="insertOneGoods" parameterType="com.mybatistest.demo.pojo.Goods" useGeneratedKeys="true" keyProperty="goodsId" >
        insert into goods(goodsName,subject,price,stock)
        values(
            #{goodsName},#{subject},#{price},#{stock}
        )
     </insert>

</mapper>

4,mapper/GoodsMapperTest.java

@ActiveProfiles("dev")
@MybatisTest()
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class GoodsMapperTest {
    @Resource
    private GoodsMapper goodsMapper;
    @Test
    @DisplayName("讀取一條記錄")
    void selectOneGoods() {
        Goods goodsOne = goodsMapper.selectOneGoods(3L);
        System.out.println(goodsOne);
        assertThat(goodsOne.getGoodsId(), equalTo(3L));
    }

    @Test
    @DisplayName("插入一條記錄並讀取")
    void insertOneGoods() {
        Goods goodsOne = new Goods();
        //goodsOne.setGoodsId(13L);
        goodsOne.setGoodsName("商品名稱xy");
        goodsOne.setSubject("商品描述");
        goodsOne.setPrice(new BigDecimal(101));
        goodsOne.setStock(13);

        int insNum = goodsMapper.insertOneGoods(goodsOne);
        assertThat(insNum, equalTo(1));

        Long goodsId = goodsOne.getGoodsId();
        //assertThat(goodsId, equalTo(14L));
        Goods goods = goodsMapper.selectOneGoods(goodsId);
        System.out.println(goods);
        //Goods goodsRet = goodsService.getOneGoodsById(14L);
        assertThat(goods.getGoodsName(), equalTo("商品名稱xy"));
    }
}

5,其他相關代碼可訪問github

 

四,測試效果

 

 

五,備注 

1,測試前需要啟動mysql數據庫

2,測試前需要准備好測試數據

3,注解不需要再寫:@Transactional

 

六,查看spring boot的版本:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.4)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM