jwebunit-net.sourceforge.jwebunit.exception.testingengineresponseexception:url处的意外状态代码[401]:[http://localhost:9999/测试/]

o2gm4chl  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(305)

我正在为用primefaces(xhtml)编写的web应用程序编写测试用例。访问urlhttp://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");
 }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题