2017-08-10 35 views
0

运行测试时出现以下错误。我试图将API响应打印到文件,但是测试失败并抛出错误。该调用的响应以JSON格式显示,并位于GZIP中。 任何想法和想法,不胜感激。获取错误io.restassured.internal.ValidatableResponseImpl无法转换为io.restassured.response.Response

错误: io.restassured.internal.ValidatableResponseImpl不能转换到io.restassured.response.Response

这里是我的代码:

package TestPackage; 

import org.testng.Assert; 
import org.testng.annotations.Test; 
import static io.restassured.RestAssured.given; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.PrintStream; 
import io.restassured.response.Response; 


public class ApiServerTest { 

    String baseQAURL = "http://some:8282/getworkitemwithtime"; 


    @Test 
    public void apiServerTest() throws FileNotFoundException{ 

     Response srvrResponse = (Response) given(). 
           param("starttime", "2017-08-10T11:17:00"). 
           param("endtime", "2017-08-10T11:17:01"). 
           when(). 
           get(baseQAURL). 
           then(). 
           log(). 
           all(); 

      System.out.println(srvrResponse.getStatusCode()); 
      Assert.assertEquals(srvrResponse.getStatusCode(), 200); 

      srvrResponse.asString(); 
      PrintStream out = new PrintStream(new FileOutputStream("C:\\Test\\output.txt")); 
      System.setOut(out); 


    } 

} 
+2

你以后得到什么'得到(baseQUARL)。然后()...'是一个'ValidatableResponse',你可以尝试使用'得到(baseQAURL) .then()。extract()。response()'返回一个'Response'对象。 –

+0

非常感谢Andrew。 get(baseQAURL).then()。extract()。response()就像一个魅力一样。我得到了答复 - 我认为使用extract()使所有的区别。感谢你的帮助。 – 9009

回答

2

评论是正确的 - 你需要提取您的响应在请求上下文之外使用。

其实你可以优化你的代码:

import io.restassured.config.LogConfig; 
import org.testng.annotations.Test; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintStream; 

import static io.restassured.RestAssured.config; 
import static io.restassured.RestAssured.given; 

public class TestLogging { 

    String baseQAURL = "https://httpbin.org/get"; 

    @Test 
    public void apiServerTest() throws FileNotFoundException { 
     config = config().logConfig(new LogConfig().defaultStream(new PrintStream(new File("C:\\Test\\output.txt")))); 

     given(). 
       param("starttime", "2017-08-10T11:17:00"). 
       param("endtime", "2017-08-10T11:17:01"). 
       when(). 
       get(baseQAURL). 
       then(). 
       log(). 
       all() 
       .assertThat().statusCode(200); 
    } 
} 
+0

我也有这个解决方案,并完美工作。我实际上有两种情况的实现 - 感谢您的建议。良好的学习和非常令人鼓舞的尝试新事物。 – 9009

相关问题