【发布时间】:2017-06-23 03:58:01
【问题描述】:
我正在开发一个解析从 HTTP 端点检索到的 XML 的系统。当我考虑如何测试我的代码时,我不希望我的单元测试实际上向实时站点发出 HTTP 请求。这似乎是一种很好的做法。
所以我把读取端点内容的代码封装在这个类中,这样我就可以用 Mockito 模拟它了。但是现在,我该如何为这个类编写单元测试呢?刚刚把问题往下推了,现在还得和它抗衡。
我可以再次包装 URL 对象,但我只是在推卸责任。
我正在尝试遵循“干净代码”中的 TDD 的 3 条法则
第一定律:在编写失败的单元测试之前,您不得编写生产代码。
第二定律:你写的单元测试不能超过足以失败的程度。
第三定律:您编写的生产代码不得超过足以通过当前失败的测试的数量。
完成这门课我已经违反了第一定律,但我不明白如何通过单元测试来解决这个问题。有什么建议吗?
/**
* Fetches the content from an HTTP Resource
*/
public class HttpFetcher {
/**
* Gets the contents of an HTTP Endpoint using Basic Auth, similar to how Postman (chrome extenstion) does.
*
* @param username Username to authenticate with
* @param password Password to authenticate with
* @param url URL of the endpoint to read.
* @return Contents read from the endpoint as a String.
* @throws HttpException if any errors are encountered.
*/
public String get(String username, String password, String url) {
URLConnection connection;
// Establish Connection
try {
connection = new URL(url).openConnection();
String credentials = encodeCredentials(username, password);
connection.setRequestProperty("Authorization", "Basic " + credentials);
} catch (MalformedURLException e) {
throw new HttpException(String.format("'%s' is not a valid URL.", e));
} catch (IOException e) {
throw new HttpException(String.format("Failed to connect to url: '%s'", url), e);
}
// Read the response
try {
String contents = readInputStream(connection.getInputStream());
return contents;
} catch (IOException e) {
throw new HttpException(String.format("Failed to read from the url: '%s' ", url), e);
}
}
private String encodeCredentials(String username, String password) {
String credentials = String.format("%s:%s", username, password);
String encodedCredentials = new String(Base64.encodeBase64(credentials.getBytes()));
return encodedCredentials;
}
private String readInputStream(InputStream is) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {
return reader.lines().collect(Collectors.joining("\n"));
}
}
}
【问题讨论】:
标签: java unit-testing