由于我一直在运行的一些集成测试,我的构建失败了。我被困在为什么它不起作用。下面是输出的一个例子: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
请不要发布错误消息的屏幕截图。请复制/粘贴错误,以便我们实际读取它。 –