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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条