Rest Assured Output:打印URL

时间:2016-05-04 23:24:24

标签: rest-assured

我想看看Rest Rest确保为测试构建了哪个URL 我的代码很香草。

ValidatableResponse response = given().
                        auth().preemptive().basic("User", "Passwd").
                        contentType(MediaType.APPLICATION_JSON).
                        log().all().
                    when().
                        get("/ping").
                    then().
                        statusCode(200);

但是输出是一个堆栈跟踪,甚至没有告诉我目标URL是什么。

checkServer
"java.lang.AssertionError: 1 expectation failed.
Expected status code <200> doesn't match actual status code <500>.

    at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
    at com.jayway.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:598)
    at com.jayway.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at com.jayway.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:760)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at com.jayway.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:120)
    at com.jayway.restassured.specification.ResponseSpecification$statusCode$0.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at com.jayway.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:128)
    at com.jayway.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:117)
    at org.mystuff.EnvironmentCheck.checkServer(EnvironmentCheck.java:43)
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:281)
    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:75)
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:121)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)
... Removed 31 stack frames



我该怎么做呢?

1 个答案:

答案 0 :(得分:2)

您的请求似乎正在使用默认主机和端口号。来自https://github.com/jayway/rest-assured/wiki/Usage#default-values:“默认情况下,REST确保在执行请求时假定主机localhost和端口8080。”

如果您想真正了解RESTAssured发送的内容,而不仅仅是推断,请参阅https://github.com/jayway/rest-assured/wiki/Usage#request-logging,其中一个引用http://hc.apache.org/httpcomponents-client-ga/logging.html以查看“实际发送的内容”。该文档在“配置示例”下有不同的选项来启用HttpClient通信记录。

从该文档中,最适合您的问题的快速解决方案发送为“启用标题线+上下文日志记录 - 最适合调试”的JVM系统属性集:

-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog 
-Dorg.apache.commons.logging.simplelog.showdatetime=true 
-Dorg.apache.commons.logging.simplelog.log.org.apache.http=DEBUG 
-Dorg.apache.commons.logging.simplelog.log.org.apache.http.wire=ERROR`