2013-08-20 58 views
0

由于我一直在运行的一些集成测试,我的构建失败了。我被困在为什么它不起作用。下面是输出的一个例子:JUnit需要特殊权限吗?

enter image description here

我使用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); 
    } 
+0

什么是系统正在测试?它是一个Web应用程序吗?如果是这样,可能是服务器的IP被允许访问您试图访问的资源,而您正在运行测试的计算机的IP不是。 –

+0

对于此测试,它会尝试访问托管在本地tomcat服务器上的资源,因此不会在Internet上进行调用。 – christopher

+0

请不要发布错误消息的屏幕截图。请复制/粘贴错误,以便我们实际读取它。 –

回答

2

我注意到你的测试输出在401错误之前显示了HTTP/1.1 500 Internal Server Error几行。我想知道根本原因是否可能藏在那里。如果我是你,我会尝试寻找关于测试中当时在服务器上发生了什么错误的更多细节,以查看它是否可能对认证问题负责(也许失败在某种登录控制器中,或导致会话被取消?)

或者:它看起来像使用Apache HttpClient library来执行请求,在issueRequest方法中。如果您需要在请求中包含身份验证凭据,那将是您需要更改的代码。这是HttpClient中的example of doing HTTP Basic authentication,如果有帮助的话。 (和more examples,如果那个没有。)

(我想第二个观察这个问题可能不是特定于JUnit。如果你需要做更多的研究,我会建议学习更多关于HttpClient ,以及这个应用程序期望浏览器发送的内容。一种可能性:当您手动执行此操作时,使用诸如Chrome Dev Tools之类的东西来查看与服务器的通信,并查看测试没有执行的重要事项,或者在做不同的。

一旦你已经找到了如何登陆,它可能是有意义的做在你的JUnit测试一个@Before方法。)

+0

这是最接近正确答案的。原来我有一个丢失的文件,这导致了各种各样的biazarre行为,包括'HTTP/1.1 401'错误。 – christopher

0

HTTP权限无关使用JUnit,您可能需要在代码本身提出请求的同时设置你的凭证给我们展示一些代码

此外,单元测试并不是真的意味着要访问互联网,它的目的是测试你的代码的小而简洁的部分, t依赖于任何外部因素,集成测试应该覆盖这一点

如果可以的话,尝试使用来嘲笑你的网络请求或PowerMock,并使它们返回您将从本地资源文件夹加载的资源(例如,测试/资源)。

+0

如果我需要在代码本身中设置凭据,它将无法在第一个地方工作吗?目前,实际的程序完美运行。我应该说这不是我的*项目。我在工作,我的老板坚持认为这些测试通过了,尽管程序是100%的功能。 – christopher

+0

没有任何代码就很难说清楚。我的意思是你需要在你的junit测试的代码中设置凭据,而不是在你正在测试的代码中。 –

+0

道歉。我将编写一个抛出'401'错误的JUnit测试样本。 – christopher