嘿,一直在努力工作在最後一天左右,但擊中磚牆。試圖單元測試這段代碼。但不知道是否需要使用EasyMock?在網上看到一些例子,但似乎使用舊技術。關於URLConnection的JUnit測試,請使用EasyMock?
public boolean verifyConnection(final String url) {
boolean result;
final int timeout = getConnectionTimeout();
if (timeout < 0) {
log.info("No need to verify connection to client. Supplied timeout = {}", timeout);
result = true;
} else {
try {
log.debug("URL: {} Timeout: {} ", url, timeout);
final URL targetUrl = new URL(url);
final HttpURLConnection connection = (HttpURLConnection) targetUrl.openConnection();
connection.setConnectTimeout(timeout);
connection.connect();
result = true;
} catch (ConnectException e) {
log.warn("Could not connect to client supplied url: " + url, e);
result = false;
} catch (MalformedURLException e) {
log.error("Malformed client supplied url: " + url, e);
result = false;
} catch (IOException e) {
log.warn("Could not connect to client supplied url: " + url, e);
result = false;
}
}
return result;
}
它只是需要在一個URL檢查其有效,如果你想嘲笑這個方法返回T或F.
你想測試這種方法,或者你想測試其他東西,需要嘲笑這種方法的創新? – Ralph 2011-04-06 10:28:19