使用Jmockit 测试 spring + mybatis plus 项目 - CRUD IService Mock样例

仅供参考
Jmockit 1.46+ junit4

package xxxx;

import xxxx;

import com.google.common.collect.Lists;
import mockit.Capturing;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Tested;
import org.junit.Before;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;

public class ServiceTest {
    @Tested
    private XXXDetailService service = new XXXDetailServiceImpl();
    @Capturing
    private XXXDetailMapper bassMapper;
    @Injectable
    private ZZZService ZZZService;

    @Injectable
    private CCCServiceService positionService;

    @Before
    public void init() throws NoSuchFieldException, IllegalAccessException {
        Field testAField = ServiceImpl.class.getDeclaredField("baseMapper");
        testAField.setAccessible(true);
        testAField.set(service, bassMapper);
    }





    @Test
    public void saveXXXDetail(@Tested CCCBalanceLog balanceLog, @Tested  CCCPositionReq req,
                                  @Tested   ArrayList<CCCPositionReq> balancePositionReqList,
                                  @Tested  CCCfolioConfig config,@Tested  ArrayList<CCCService> list,@Tested CCCService position) {
        req.xxxxxx(5.5);

        new Expectations(ZZZService) {
            {
                //设置mock逻辑
                positionService.getCurrentPositions(balanceLog.getPortfolioId(), balanceLog.getStartDate());
                result=list;
                ZZZService.completeddddddBasicInfo(balancePositionReqList);
                result=balancePositionReqList;
            }
        };
        service.saveXXXDetail(balanceLog,balancePositionReqList,config);
    }
}

posted @ 2023-02-22 17:21  大哥超帅  阅读(205)  评论(0编辑  收藏  举报