【发布时间】:2020-06-30 15:14:06
【问题描述】:
我正在尝试为以下代码 sn-p 编写测试用例。但是模拟没有按预期工作。
Map<String, String> request = new HAshMap<String, String>();
URL url = new URL("sampleurl");
HttpUrlConnection connection = (HttpUrlConnection) url.openConnection();
if(connection != null){
connection.setDOutput(true);
OutputStreamReader out = new OutputStreamReader(connection.getOutputStream());
out.write(new Gson().toJson(request);
out.close();
int responseCode = connection.getResponseCode();
}
我已尝试使用以下测试代码 sn-p 进行模拟,但 Mock 无法正常工作,我做错了
URL url = PowerMock.createNiceMock(URL.class);
HttpsUrlConnection httpConn = PowerMock.createNiceMock(HttpsUrlConnection.class);
EasyMock.expect(url.openConnection()).andReturn(httpConn);
EasyMock.expect(httpConn.getResponseCode()).andReturn(500);
【问题讨论】:
-
你创建了新的 url:
URL url = new URL("sampleurl");你没有在被测方法中使用你的模拟实例。