qq_33777968 2016-02-27 04:21 采纳率: 0%
浏览 4487

PowerMockito来mock 构造函数的问题

public class HttpProtocolHandler {

private static HttpProtocolHandler httpProtocolHandler  = new HttpProtocolHandler();

public static HttpProtocolHandler getInstance() {
    return httpProtocolHandler;
}

   /**
 * 私有的构造方法
 */
private HttpProtocolHandler() {
    connectionManager= new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(200);
}

public TwoTuple<Integer,String> execute(HttpUriRequest request) throws Exception {

   //中间省略
    return new TwoTuple<Integer,String>(HttpStatus.SC_OK,null);
}
}

被测代码如上,测试代码如下,但是mock不生效。
我现在要mock HttpProtocolHandler.getInstance().execute(httpPost)的返回结果,测试代码哪里不对,新手求大神帮助解决,谢谢

@RunWith(PowerMockRunner.class)

@PrepareForTest(HttpProtocolHandler.class)
public class httpTest(){

@Test
public void test(){

    TwoTuple<Integer,String> rep = new TwoTuple<Integer, String>(1,"test1234567");

    HttpProtocolHandler mock = PowerMockito.spy(HttpProtocolHandler.getInstance());

    PowerMockito.whenNew(HttpProtocolHandler.class).withNoArguments().thenReturn(mock);
    PowerMockito.doReturn(rep).when(mock).execute((HttpUriRequest)Mockito.any());

    String url = "www";
    HttpPost httpPost = new HttpPost(url);
    TwoTuple<Integer,String> result11 = HttpProtocolHandler.getInstance().execute(httpPost);
    logger.info(result11.toString())
}

}

  • 写回答

1条回答 默认 最新

  • devmiao 2016-02-27 23:00
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大