<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.6</version>
准备工作:@Runwith PowerMockRunner.class
@PrepareForTest 所有要mock的静态类
@RunWith(PowerMockRunner.class)
@PrepareForTest({RequestConvertUtil.class, JsonUtil.class,IPUtil.class,BaseInterfaceUtil.class,RSACoder.class,CookieUtils.class,URLEncoder.class})
public class LoginControllerTest {
mockStatic方法:mock有静态方法的类
mockStatic(BaseInterfaceUtil.class);
when(BaseInterfaceUtil.getServiceEncryptkeyByCode("autoLogin")).thenReturn(rsaKeyMap);
具体参照
http://www.cnblogs.com/hunterCecil/p/5721468.html
http://huangyunbin.iteye.com/blog/2176728
package com.suning.finance.phonepad.controller.bankcard;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.suning.finance.phonepad.SNConstants;
import com.suning.finance.phonepad.baseserver.bean.ServiceBean;
import com.suning.finance.phonepad.baseserver.service.StaticBaseService;
import com.suning.finance.phonepad.bizservices.baseservice.InterBaseService;
import com.suning.finance.phonepad.bizservices.ppp.PppService;
import com.suning.finance.phonepad.encrypt.PBECoder;
import com.suning.finance.phonepad.util.SensitiveDataUtil;
import com.suning.finance.phonepad.util.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import java.util.Map;
import static com.suning.epp.puma.BeanUtil.toGeneric;
import static com.suning.epp.puma.PumaAssert.assertCompare;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
* TransferServlet Tester.
* @author <Authors name>
* @since <pre>02/09/2017</pre>
* @version 1.0
@RunWith(PowerMockRunner.class)
@PrepareForTest({StringUtil.class})
public class TransferServletTest {
private final static Logger LOGGER = LoggerFactory.getLogger(TransferServletTest.class);
@Rule
public MockitoRule rule = MockitoJUnit.rule();
@InjectMocks
private TransferServlet transferServlet;
@Mock
private PppService pppService;
@Mock
InterBaseService service;
@Test
public void transfer_sdk_create_order() throws Exception {
MockHttpServletRequest givenRequest = new MockHttpServletRequest();
givenRequest.setParameter("service","transfer_sdk_create_order");
MockHttpServletResponse givenResponse = new MockHttpServletResponse();
MockHttpSession givenSession = new MockHttpSession();
givenSession.setAttribute(SNConstants.FTIS_ACCOUNT_NO,"1234567980");
Object expectResult = toGeneric("TransferSDKSuccess.json",new TypeReference<Object>(){});
Map<String, Object> transferToCardMap =toGeneric("TransferSDKSuccess.json",new TypeReference<Map>(){});
when(pppService.transferToCardCreateOrder(anyMap())).thenReturn(transferToCardMap);
mockStatic(StringUtil.class);
when(StringUtil.getInfoParamStringByPBEData(givenRequest)).thenReturn(anyString());
Object result = transferServlet.transfer(givenRequest,givenResponse);
LOGGER.info("expectResult:{}", JSON.toJSON(expectResult));
LOGGER.info("result:{}", JSON.toJSON(result));
assertCompare(result, expectResult);