0

我正在为使用 Primefaces(xhtml) 编写的 Web 应用程序编写测试用例。要访问 URL http://localhost:8080/Test/,它会询问用户名和密码。以下是我尝试访问 URL 时的错误

net.sourceforge.jwebunit.exception.TestingEngineResponseException: unexpected status code [401] at URL: [http://localhost:9999/Test/]
    at net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl.gotoPage(HtmlUnitTestingEngineImpl.java:261)
    at net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl.beginAt(HtmlUnitTestingEngineImpl.java:213)
    at net.sourceforge.jwebunit.junit.WebTester.beginAt(WebTester.java:234)
    at com.test.security.JWebTest.testFormPage(JWebTest.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 401 Unauthorized for http://localhost:9999/Test/
    at com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebClient.java:535)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:360)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:415)
    at net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl.gotoPage(HtmlUnitTestingEngineImpl.java:256)
    ... 27 more

以下是我尝试过的代码:

 private WebTester tester;

 @Before
 public void prepare(){
     tester = new WebTester();
     final WebClient xhtmlBrowser = new WebClient(BrowserVersion.CHROME);
     xhtmlBrowser.addRequestHeader("ACCEPT", "text/html,application/xhtml+xml");
     final DefaultCredentialsProvider userCredentials = new  DefaultCredentialsProvider();
     userCredentials.addCredentials("aaaa", "bbbb");
     try {
         final WebResponse xhtmlResponse = xhtmlBrowser.getPage("http://localhost:9999/Test/demo.xhtml").getWebResponse();
         System.out.println("Response ------ "+xhtmlResponse);
     } catch (FailingHttpStatusCodeException | IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
     tester.setBaseUrl("http://localhost:9999/Test/demo.xhtml");
 }
4

0 回答 0