单元测试(一)——SpringBoot建立单元测试

在Spring项目中,对于controller、service、dao各层都需要建立单元测试项。对应不同的分层,我们可以使用junit和mock不同的方式。然而有些情况会需要启动spring容器来测试业务逻辑在容器内能否正常运行,针对此情况可参考如下单元测试方式。

SpringBoot增加依赖

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

建立测试基类

  • 新建JUnitBaseTest.java
package com.lucas.device;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.util.StopWatch;

import lombok.extern.slf4j.Slf4j;

/**
 * <Description> <br>
 * 
 * @author xubin<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2018年9月27日 <br>
 */

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class JUnitBaseTest {

    /**
     * sw 计时器<br>
     */
    public static StopWatch sw = null;

    /**
     * Description: <br>
     * 
     * @author xubin<br>
     * @taskId <br>
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值