码迷,mamicode.com
首页 > 其他好文 > 详细

第一次使用Mockito进行测试

时间:2015-03-31 18:10:12      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

第一次使用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

第一次使用Mockito进行测试

标签:

原文地址:http://blog.csdn.net/ro_wsy/article/details/44781491

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!