第一次使用Mockito进行测试

本文详细介绍了如何使用Mockito进行单元测试的实践过程,包括设置mock对象、验证方法调用、处理异常情况等关键步骤,通过具体的代码示例展示了测试的成功案例以及异常处理策略。

第一次使用Mockito进行测试,记录一下

package com.hxt.account.mpos.service.impl;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import com.hxt.account.exception.RegisterException;
import com.hxt.account.mpos.service.MposRegisterService;
import com.hxt.account.persistence.MerchantInfo;
import com.hxt.account.persistence.UserInfo;
import com.hxt.account.repository.MerchantInfoRepository;
import com.hxt.account.repository.UserInfoRepository;
import com.hxt.account.service.RegisterService;
import com.hxt.common.internal.UserType;
import com.hxt.common.internal.exception.BlankArgumentException;
import com.hxt.id.generator.service.MerchantInfoIdGenerator;
import com.hxt.id.generator.service.UserInfoIdGenerator;

@RunWith(MockitoJUnitRunner.class)
public class MposRegisterServiceImplTest {

    @Mock
    private RegisterService registerService;

    @Mock
    private UserInfoRepository userInfoRepository;

    @Mock
    private UserInfoIdGenerator userInfoIdGenerator;

    @Mock
    private MerchantInfoIdGenerator merchantInfoIdGenerator;

    @Mock
    private MerchantInfoRepository merchantInfoRepository;

    @InjectMocks
    MposRegisterService mposRegisterService = new MposRegisterServiceImpl();

    @Test
    public void testRegisterSuccess() throws RegisterException, BlankArgumentException {

        when(registerService.userExists(isA(String.class))).thenReturn(false);
        when(userInfoRepository.save(isA(UserInfo.class))).thenAnswer(new Answer<UserInfo>() {

            @Override
            public UserInfo answer(InvocationOnMock invocation) throws Throwable {

                UserInfo info = new UserInfo();
                info.setUserId("001");
                return info;
            }
        });
        when(userInfoIdGenerator.generate(isA(UserType.class))).thenReturn("001");
        when(merchantInfoIdGenerator.generate()).thenReturn("0001");
        when(merchantInfoRepository.save(isA(MerchantInfo.class))).thenAnswer(new Answer<MerchantInfo>() {

            @Override
            public MerchantInfo answer(InvocationOnMock invocation) throws Throwable {

                MerchantInfo info = new MerchantInfo();
                info.setMerchantId("0001");
                return info;
            }
        });
        String merchantId = mposRegisterService.register("13100001111", "123456");
        verify(registerService).userExists(isA(String.class));
        verifyNoMoreInteractions(registerService);
        verify(userInfoRepository).save(isA(UserInfo.class));
        verifyNoMoreInteractions(userInfoRepository);
        verify(userInfoIdGenerator).generate(isA(UserType.class));
        verifyNoMoreInteractions(userInfoIdGenerator);
        verify(merchantInfoIdGenerator).generate();
        verifyNoMoreInteractions(merchantInfoIdGenerator);
        verify(merchantInfoRepository).save(isA(MerchantInfo.class));
        verifyNoMoreInteractions(merchantInfoRepository);
        assertTrue("merchant info id is not correct", merchantId.equals("0001"));
    }

    @Test(expected = BlankArgumentException.class)
    public void testRegisterThrowBlankArgumentException() throws BlankArgumentException, RegisterException {

        mposRegisterService.register("", "");
    }

    @Test(expected = RegisterException.class)
    public void testRegisterThrowRegisterException() throws RegisterException, BlankArgumentException {

        when(registerService.userExists(isA(String.class))).thenReturn(true);
        mposRegisterService.register("13100001111", "123456");
        verify(registerService).userExists(isA(String.class));
        verifyNoMoreInteractions(registerService);
    }

}

that's all

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值