Mockito + Junit5 提升java 代码覆盖率

Service 代码

package com.nz.mockito.Impl;

import com.nz.mockito.RegistrationService;
import com.nz.mockito.mapper.SaleDao;
import com.nz.mockito.mapper.UserDao;
import com.nz.mockito.object.SalesRep;
import com.nz.mockito.object.User;
import com.nz.mockito.util.FindUtils;

import javax.xml.bind.ValidationException;
import java.io.IOException;

/**
 * @author Sherry
 * @date 2024/1/24
 */
public class RegistrationServiceImpl implements RegistrationService {

    SaleDao saleDao = new SaleDao();
    UserDao userDao = new UserDao();

    @Override
    public User register(String name, String phone) throws Exception {
        if(name== null || name.length() == 0){
            throw new ValidationException("name not empty");
        }

        if(phone == null || !isValid(phone)){
            throw new ValidationException("phone format error");
        }


        String areaCode = FindUtils.getAreaCode(phone);
        String operatorCode = FindUtils.getOperatorCode(phone);

        User user;

        try{
            SalesRep rep =  saleDao.findRep(areaCode, operatorCode);

            //
            user = userDao.save(name, phone, rep.getRepId());
        }catch (Exception e){
            throw new IOException(e);
        }


        return user;
    }

    private boolean isValid(String phone) {
        return true;
    }
}

Service Test类

package com.nz.mockito.Impl;

import com.nz.mockito.mapper.SaleDao;
import com.nz.mockito.mapper.UserDao;
import com.nz.mockito.object.User;
import com.nz.mockito.util.FindUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;

import javax.xml.bind.ValidationException;

/**
 * @author Sherry
 * @date 2024/1/24
 */
class RegistrationServiceImplTest {

    @InjectMocks
    @Spy
    private RegistrationServiceImpl registrationService;

    @Mock
    private UserDao userDao;

    @Mock
    private SaleDao saleDao;

    @BeforeEach
    void setUp(){
        MockitoAnnotations.openMocks(this);
    }


    @Test
    void register() throws Exception {
        String name = null;
        String phone = "1234";
        try{
        registrationService.register(name, phone);
        Assertions.fail("fail here");
        }catch (Exception e){
            Assertions.assertTrue(e instanceof ValidationException);
        }

        name = "nz";
        phone = null;
        try{
        registrationService.register(name, phone);}
        catch (Exception e){
            Assertions.assertTrue(e instanceof ValidationException);
        }

        phone = "1234";
        MockedStatic<FindUtils> utilStatic = Mockito.mockStatic(FindUtils.class);
        utilStatic.when(() -> FindUtils.getAreaCode(Mockito.anyString())).thenReturn("a");
        utilStatic.when(() -> FindUtils.getOperatorCode(Mockito.anyString())).thenReturn("b");

        Mockito.when(saleDao.findRep("a","b")).thenCallRealMethod();
        Mockito.when(userDao.save(name, phone, "ECHO")).thenCallRealMethod();
        User user = registrationService.register(name, phone);
        Assertions.assertEquals("ECHO", user.getRepId());

    }
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值