JMockit test example

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 //The service being tested
    LogInfoServiceImpl service;
    
    @Injectable //Services that need to be depended on

    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<>();                 logTypeProperties.getSystem();             {
        new Expectations() {//The method expected to be called and the value returned by the simulation


                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);//Use assertions to test
    }
}