标签:
第一次使用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); } }
标签:
原文地址:http://blog.csdn.net/ro_wsy/article/details/44781491