SpringBoot之单元测试

问题

最近项目组来一个QA,说是要加强项目质量管理,建议我们实践单元测试。这次就以SpringBootTest来做单元测试。

步骤

SpringBootTest项目结构

SpringBootTest项目结构
注意这里测试类,必须与main源代码中的包空间保持一致,保持一致才能够让测试类能够正常调用需要被测试的类。

测试配置文件

测试application.yml

# ****** H2 In Memory Database Connection Info *******
spring:
  profiles:
    active: local-test
  application:
    name: myapp
  datasource: # use in-memory db for unit testing
    url: jdbc:h2:mem:mydb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
    driver-class-name: org.h2.Driver
    continue-on-error: false
    platform: h2
    schema: classpath:/db/schema.sql
  h2:
    console:
      enabled: true
  output:
    ansi:
      enabled: always

#mybatis
mybatis:
  mapper-locations: classpath:/mapper/*.xml
  type-aliases-package: db.entity

这里主要使用h2内存数据库来进行mapper持久层的单元测试,这里使用了mybatis需要进行一些xml的映射文件的指定。

schema.sql

CREATE TABLE IF NOT EXISTS my_tb
(
    id                        bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键id' ,
    buyer_id                  bigint(20) COMMENT '买家用户id',
    seller_id                 bigint(20) COMMENT '卖家id',
    material_id bigint(20) COMMENT '模板id'
) ENGINE=InnoDB COMMENT='转码模板';

为H2准备数据库初始化脚本,就是一个建表sql。

Java

package com.xxx.mapper;

import db.entity.MaterialSpecificationSend;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.NONE)
@RunWith(SpringRunner.class)
public class MaterialSpecificationSendMapperTest {
    @Resource
    private MaterialSpecificationSendMapper materialSpecificationSendMapper;

    private MaterialSpecificationSend materialSpecificationSend;

    @Before
    public void setUp() {
        // 测试之前准备工作
        materialSpecificationSend = MaterialSpecificationSend.builder()
                .sellerId(8L)
                .materialSpecificationId(8L)
                .buyerId(8L)
                .build();
        // sanity check
        materialSpecificationSendMapper.deleteAll();
    }

    @Test
    public void insert(){
        materialSpecificationSendMapper.insert(materialSpecificationSend);
        // 是否达到预期的不为空
        assertNotNull(materialSpecificationSend.getId());
    }

    @Test
    public void findById(){
        materialSpecificationSendMapper.insert(materialSpecificationSend);
        assertNotNull(materialSpecificationSend.getId());
        assertNotNull(materialSpecificationSendMapper.findById(materialSpecificationSend.getId()));
    }

    @Test
    public void deleteByMaterialSpecificationId(){
        materialSpecificationSendMapper.insert(materialSpecificationSend);
        assertNotNull(materialSpecificationSend.getId());
        materialSpecificationSendMapper.deleteByMaterialSpecificationId(materialSpecificationSend.getId());
        assertNotNull(materialSpecificationSendMapper.findById(materialSpecificationSend.getId()));
    }

    @Test
    public void deleteAll(){
        materialSpecificationSendMapper.insert(materialSpecificationSend);
        assertNotNull(materialSpecificationSend.getId());
        materialSpecificationSendMapper.deleteAll();
        // 是否达到预期的为
        assertNull(materialSpecificationSendMapper.findById(materialSpecificationSend.getId()));
    }


    @After
    public void destroy() {
        // 测试之后要处理的事情
        materialSpecificationSendMapper.deleteAll();
    }
}

这里主要就是SpringBootTest的一些注解的使用,已经Junit的使用。

运行单元测试

运行单元测试

Maven Surefire Plugin

这个插件主要是用来生成单元测试报告。只需要在需要生成报告的maven模块的pom.xml加入如下依赖即可:

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                    <version>5.4.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

然后使用,mvn test命令就可以在target/surefire-reports/*.xml了解测试报告。

总结

这里主要就是运用SpringBootTest,Junit的相关注解来进行单元测试。

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值