当前位置: 首页 > 工具软件 > EasyMock.NET > 使用案例 >

service层单元测试+easymock

佘缪文
2023-12-01



   #####################################################################

                                  service层+easymock单元测试处理

    #####################################################################
package cn.**.co.service.impl;

import cn.**.dao.mapper.companyUser.CompanyUserInfoMapper;
import cn.**.co.model.UserBase;
import cn.**.co.service.ICoLoginService;
import cn.**.co.service.IUserBaseService;
import cn.**.co.web.exception.BusinessException;
import cn.**.model.CompanyUserinfo;
import **.makersoft.hards.model.MainDbKeyEnum;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.*;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import static jdk.nashorn.api.scripting.ScriptUtils.unwrap;
import static junit.framework.TestCase.assertTrue;
import static org.easymock.EasyMock.isA;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-test.xml"})
@Transactional(transactionManager = "transactionManager")
@Rollback(value = true)
public class CoLoginServiceImplTest {

    @Resource
    private ICoLoginService instance;

    public CoLoginServiceImplTest(){

    }


    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    private CompanyUserinfo companyUserinfo1 = new CompanyUserinfo();
    private CompanyUserinfo companyUserinfo2 = new CompanyUserinfo();
    private CompanyUserinfo companyUserinfo3 = new CompanyUserinfo();
    private CompanyUserinfo companyUserinfo4 = new CompanyUserinfo();
    private CompanyUserinfo companyUserinfo5 = new CompanyUserinfo();

    @Before
    public void setUp() {
        companyUserinfo1.setDomainName("test1");
        companyUserinfo1.setName("test1");
        companyUserinfo2.setDomainName("test2");
        companyUserinfo2.setName("test2");
        companyUserinfo2.setId(100393);
        companyUserinfo3.setDomainName("test3");
        companyUserinfo3.setName("test3");
        companyUserinfo4.setStatusInt(0);
        companyUserinfo4.setGenerated(0);
        companyUserinfo5.setCompersonTel("**********");
        companyUserinfo5.setDomainName("test5");
        companyUserinfo5.setPassword("123456");
        IMocksControl control = EasyMock.createControl();
        CompanyUserInfoMapper companyUserInfoMapper = control.createMock(CompanyUserInfoMapper.class);
        IUserBaseService userBaseService = control.createMock(IUserBaseService.class);
        EasyMock.expect(companyUserInfoMapper.updateStatus_SP(isA(CompanyUserinfo.class))).andReturn(1).times(3);
        EasyMock.expect(companyUserInfoMapper.insertSelective_SP(companyUserinfo1)).andReturn(0).times(1);
        EasyMock.expect(companyUserInfoMapper.findCoUserInfoNum_SP(isA(CompanyUserinfo.class))).andReturn(0).times(1);
        EasyMock.expect(companyUserInfoMapper.findByUuid_SP(MainDbKeyEnum.FOREMOST.getDbKey(),"D70A42E9A77E4F4D9CD4B4B8C05F5A80")).andReturn(companyUserinfo4).times(1);
        EasyMock.expect(companyUserInfoMapper.insertSelective_SP(companyUserinfo2)).andReturn(1).times(1);
        EasyMock.expect(companyUserInfoMapper.insertSelective_SP(companyUserinfo5)).andReturn(1).times(1);
        EasyMock.expect(companyUserInfoMapper.insertSelective_SP(companyUserinfo3)).andThrow(new BusinessException()).times(1);
        EasyMock.expect(userBaseService.saveSelective(isA(UserBase.class))).andReturn(1).times(2);
        control.replay();
        ReflectionTestUtils.setField(unwrap(instance),"companyUserInfoMapper",companyUserInfoMapper,CompanyUserInfoMapper.class);
        ReflectionTestUtils.setField(unwrap(instance),"userBaseService",userBaseService,IUserBaseService.class);
    }

    @After
    public void tearDown() {
    }

    @Test
    public void companyUserRegister(){
        int res = instance.companyUserRegister(companyUserinfo1,null);
        assertTrue(res == 0);
    }

    @Test
    public void companyUserRegister1(){
        int res = instance.companyUserRegister(companyUserinfo2,null);
        assertTrue(res == 1111);
    }

    @Test
    public void companyUserRegister2(){
        try{
            instance.companyUserRegister(companyUserinfo3,null);
        }catch (Exception ex){
        }
    }

    @Test
    public void createNewTable()throws Exception{
        instance.createNewTable("****",111111);
    }


    @Test
    public void initUserRoleAndProData(){
        instance.initUserRoleAndProData("*******","*****",11111);
    }


    @Test
    public void companyRegister(){
        try{
            instance.companyRegister(companyUserinfo5);
        }catch(Exception ex){

        }
    }

}
参考文献:

https://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/

https://blog.csdn.net/vking_wang/article/details/9170979


 类似资料: