JMockit测试实例

import java.util.ArrayList;
import java.util.List;

import mockit.integration.junit4.JMockit;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import mockit.*;
import org.junit.runner.RunWith;

@RunWith(JMockit.class)
public class LogServiceTest{
    @Tested //被测试的服务
    LogInfoServiceImpl service;
    
    @Injectable //需要依赖的服务

    LogInfoMapper logInfoMapper;
    
    private LogListReq req = new LogListReq();
    
    @Before
     public void init(){
        req.setType(1);
        req.setFrom("2017-11-01");
        req.setTo("2017-12-01");
        req.setPageNo(1);
        req.setPageSize(10);
     }
    
    @Test
    public void testLogListService(@Injectable LogManagerTypeProperties logTypeProperties) {
        Page<LogInfo> page = new Page<LogInfo>();
        Wrapper<LogInfo> wrapper = new EntityWrapper<>();
        List<LogInfo> results = new ArrayList<>();
        new Expectations() {//期望被调用的方法以及模拟返回的值
            {
                logTypeProperties.getSystem();
                result = "a";
                logInfoMapper.selectPage(withAny(page), withAny(wrapper));
                result = results;
            }
        };
        service.logList(req);//被测试的方法
    }
    
    @Test
    public void testLogExportService(@Injectable LogManagerTypeProperties logTypeProperties) {
        LogExportReq req = new LogExportReq(1, null, null, null, null, null, null);
        Wrapper<LogInfo> wrapper = new EntityWrapper<>();
        List<LogInfo> results = new ArrayList<>();
        LogInfo logInfo = new LogInfo();
        results.add(logInfo);
        new Expectations() {
            {
                logTypeProperties.getSystem();
                result = "a";
                logInfoMapper.selectList(withAny(wrapper));
                result = results;
                logTypeProperties.getSystemExcelTitle();
                result = "log.key.export.opbusiness,log.key.export.logtype,log.key.export.opobj,log.key.export.desc,log.key.export.optime";
            }
        };
        try {
            service.createLogExcel(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void testUserListService() {
        UserInfo user = new UserInfo();
        user.setId(1L);
        user.setIndexCode("000011");
        user.setStatus(0L);
        List<UserInfo> list = new ArrayList<>();
        list.add(user);
        new Expectations(UserInfo.class) {
            {
                user.selectAll();
                result = list;
            }
        };
        service.userList();
    }
    
    @Test
    public void testModuleList(@Injectable LogManagerTypeProperties logTypeProperties) {
        new Expectations() {
            {
                logTypeProperties.getSystem();
                result = "xxx";
            }
        };
        service.moduleList(1);
    }
    
    @Test
    public void testActionList() {
        new Expectations() {
            {
//                logInfoService.actionList();
//                result = null;
            }
        };
        Assert.assertTrue(service.actionList() != null);//使用断言来进行测试
    }
}

猜你喜欢

转载自my.oschina.net/ffse54s/blog/1805701