一、运行测试(JUnit)
方法:运行真实的项目环境(启动spring应用、连接数据库、加载各种环境)
示例代码:
package com.ewt360.psychology.service.biz.dal.mapper; import com.ewt360.psychology.service.biz.dal.entity.ReportDataTempEntity; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.transaction.annotation.Transactional; import java.util.List; import static org.junit.Assert.*; /** * Title: ReportDataTempMapperTest * Description: 单元测试 * author: xu jun * date: 2019/4/18 */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest @WebAppConfiguration @Transactional @Slf4j public class ReportDataTempMapperTest { @Autowired ReportDataTempMapper reportDataTempMapper; @Before public void setUp() { log.info("Test start.........."); } @Test public void selectData() { //先插后查,由于开启了事务,不会对数据库造成影响 ReportDataTempEntity testEntity = new ReportDataTempEntity(); testEntity.setType(99); testEntity.setData("UC test"); reportDataTempMapper.insertOne(testEntity); List<ReportDataTempEntity> reportDataTempEntities = reportDataTempMapper.selectData(testEntity); Assert.assertEquals(1, reportDataTempEntities.size()); } }
优点:
1.可以跑真实的代码,适合做冒烟测试。如DAO、core、service层的接口测试
缺点
1.需要加载环境,启动慢
2.依赖于真实的环境和数据库,需要配置加载环境和数据库数据(好处是可以做冒烟测试,坏处是依赖外部环境,不能只测某个单元的逻辑)
注意点:
1.某些VM的启动参数,需要在本地配置(如阿波罗配置系统)。可以在idea的单元测试启动项里设置
2.由于单元测试的idea启动项设置里,没有spring boot的active profiles这个选项,所以无法读取bootstrap-dev.properties里的配置。所以要写在bootstrap.properties里
二、mock测试(PowerMock)
方法:使用mock框架,在单元测试的时候,通过打桩来mock方法所需的对象
代码:
package com.ewt360.psychology.service.biz.dal.domain; import com.alibaba.fastjson.JSON; import com.ewt360.psychology.service.biz.dal.entity.ReportDataTempEntity; import com.ewt360.psychology.service.biz.dal.mapper.ReportDataTempMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.powermock.modules.junit4.PowerMockRunner; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Title: ReportDataTempDomainMockTest * Description: 单元测试(Mock) * author: xu jun * date: 2019/4/18 */ @RunWith(PowerMockRunner.class) public class ReportDataTempDomainMockTest { @InjectMocks private ReportDataTempDomain reportDataTempDomain; @Mock private ReportDataTempMapper reportDataTempMapper; @Test public void getLastUpdateTime() { //stub Mockito.when(reportDataTempMapper.selectData(Mockito.any())).thenReturn(buildReportDataTempEntities()); Date lastUpdateTime = reportDataTempDomain.getLastUpdateTime(); Assert.assertNotNull(lastUpdateTime); } private List<ReportDataTempEntity> buildReportDataTempEntities() { List<ReportDataTempEntity> result = new ArrayList<>(); ReportDataTempEntity entity = new ReportDataTempEntity(); entity.setType(99); entity.setData(JSON.toJSONString(new Date())); result.add(entity); return result; } }
有点:
1.不依赖于环境,只测最小粒度的方法逻辑
2.运行速度快
缺点:
1.单元测试所需的数据,全靠模拟。实际生产环境里的数据可能会不同(不过从单元测试的角度讲,没关系)
2.适用于测试复杂的业务逻辑,对外部依赖的因素(环境变量,配置)无法测试
总结:
1.运行测试适用于冒烟测试和从下向上写的接口自测(也可以测facade层的HTTP请求)
2.Mock测试适用于测单一的逻辑做单元测试