由於我一直在運行的一些集成測試,我的構建失敗了。我被困在爲什麼它不起作用。下面是輸出的一個例子:JUnit需要特殊權限嗎?
我使用Maven來先建,然後調用JUnit測試。我在每次測試中都看到這個消息,我認爲這是造成構建失敗的原因。在我看來,這意味着需要設置一些權限/驗證參數。我會在JUnit中做什麼?
編輯
@Test
public void testXmlHorsesNonRunners() throws Exception {
String servletUrl = SERVER + "sd/date/2013-01-13/horses/nonrunners";
Document results = issueRequest(servletUrl, APPLICATION_XML, false);
assertNotNull(results);
// debugDocument(results, "NonRunners");
String count = getXPathStringValue(
"string(count(hrdg:data/hrdg:meeting/hrdg:event/hrdg:nonrunner/hrdg:selection))",
results);
assertEquals("non runners", "45", count);
}
如果可以,儘量忽略的細節。實際上,這是提出請求。這是使用issueRequest
方法的測試示例。這個方法是造成HTTP請求的原因。 (這是一個大的方法,這就是爲什麼我沒有張貼它最初,我會盡量做到儘可能地易讀。否認
logger.info("Sending request: " + servletUrl);
HttpGet httpGet = null;
// InputStream is = null;
DefaultHttpClient httpclient = null;
try {
httpclient = new DefaultHttpClient();
doFormLogin(httpclient, servletUrl, acceptMime, isIrishUser);
httpGet = new HttpGet(servletUrl);
httpGet.addHeader("accept", acceptMime);
// but more importantly now add the user agent header
setUserAgent(httpGet, acceptMime);
logger.info("executing request" + httpGet.getRequestLine());
// Execute the request
HttpResponse response = httpclient.execute(httpGet);
// Examine the response status
StatusLine statusLine = response.getStatusLine();
logger.info(statusLine);
switch (statusLine.getStatusCode()) {
case 401:
throw new HttpResponseException(statusLine.getStatusCode(),
"Unauthorized");
case 403:
throw new HttpResponseException(statusLine.getStatusCode(),
"Forbidden");
case 404:
throw new HttpResponseException(statusLine.getStatusCode(),
"Not Found");
default:
if (300 < statusLine.getStatusCode()) {
throw new HttpResponseException(statusLine.getStatusCode(),
"Unexpected Error");
}
}
// Get hold of the response entity
HttpEntity entity = response.getEntity();
Document doc = null;
if (entity != null) {
InputStream instream = entity.getContent();
try {
// debugContent(instream);
doc = documentBuilder.parse(instream);
} catch (IOException ex) {
// In case of an IOException the connection will be released
// back to the connection manager automatically
throw ex;
} catch (RuntimeException ex) {
// In case of an unexpected exception you may want to abort
// the HTTP request in order to shut down the underlying
// connection and release it back to the connection manager.
httpGet.abort();
throw ex;
} finally {
// Closing the input stream will trigger connection release
instream.close();
}
}
return doc;
} finally {
// Release the connection.
closeConnection(httpclient);
}
什麼是系統正在測試?它是一個Web應用程序嗎?如果是這樣,可能是服務器的IP被允許訪問您試圖訪問的資源,而您正在運行測試的計算機的IP不是。 –
對於此測試,它會嘗試訪問託管在本地tomcat服務器上的資源,因此不會在Internet上進行調用。 – christopher
請不要發佈錯誤消息的屏幕截圖。請複製/粘貼錯誤,以便我們實際讀取它。 –