如果响应json是一个空列表,我该如何与rest-assured(2.4.0)一起检查?
鉴于响应[](带标题content-type=application/json)我试过:
.body(Matchers.emptyArray()) // expected: an empty array, actual: []
.body("/", Matchers.emptyArray()) // invalid expression /
.body(".", Matchers.emptyArray()) // invalid expression .
Run Code Online (Sandbox Code Playgroud) 我有放心的POST请求的问题.
此代码有效:
given().contentType(ContentType.JSON).body("{\"key\": \"val\"}").
when().post(url + resource).then().assertThat().statusCode(200).body("otherVal", equalTo(otherVal));
Run Code Online (Sandbox Code Playgroud)
但我试图使用param()或parameter()类似的方法:
这个给出:
given().parameter("key", "val").
when().post(url + resource).then().assertThat().statusCode(200);
Run Code Online (Sandbox Code Playgroud)
Expected status code <200> doesn't match actual status code <415>.
这个:
given().parameter("key", "val").
when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));
Run Code Online (Sandbox Code Playgroud)
java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but no content-type was defined in the response.
Try registering a default parser using:
RestAssured.defaultParser(<parser type>);
还有这个:
RestAssured.defaultParser = Parser.JSON;
given().parameter("key", "val").
when().post(url + resource).then().assertThat().body("otherVal", equalTo(otherVal));
Run Code Online (Sandbox Code Playgroud)
java.lang.IllegalArgumentException: The JSON input text should …
下面的测试用例给了我一个连接拒绝异常,当我评论方法体时,它是成功的.所以我相信端口没有问题,但我确定无法确定调用的问题.即使我与其他客户端一起检查,它也能成功地将书籍作为json返回.
@Test
public void testMe() {
get("/book").then().assertThat().contentType(ContentType.JSON);
}
Run Code Online (Sandbox Code Playgroud)
控制器如下所示,
@RestController
@RequestMapping("/book")
public class BookController {
//Other crud api's are there
@RequestMapping(method = RequestMethod.GET)
public Map<String, Object> getAllBooks(){
List<Book> books = bookRepository.findAll();
Map<String, Object> response = new LinkedHashMap<String, Object>();
response.put("totalBooks", books.size());
response.put("books", books);
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
异常追踪,
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:117)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:177)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863) …Run Code Online (Sandbox Code Playgroud) 如何使用Java中的Rest-Assured向需要证书的端点进行GET调用.我有证书作为.pem格式.在PEM文件中有证书和私钥.
我正在尝试使用 测试在我的控制器类中定义的 REST api REST Assured v4.3.0,但是我java.lang.AbstractMethodError在运行测试时得到了。我知道发生此错误是因为我正在调用一个抽象方法,但我很难解决它。
似乎错误是由于.body(is(equalTo("success")))in发生的,SampleControllerTest.java因为当我删除此行时,测试成功。我尝试了一些方法来解决它,但没有取得任何成功:
io.rest-assured/spring-mock-mvcorg.hamcrest.Matchers.*和org.hamcrest.CoreMatchers.*)org.hamcrest/hamcrest在 pom 文件中显式添加依赖项这是我的代码供您参考:
代码结构:
test
|- src/
| |- main/
| | |- java/
| | | |- org/
| | | | |- example/
| | | | | |- Application.java
| | | | | |- SampleController.java
| |- test/
| | |- java/
| | | |- org/
| | | | …Run Code Online (Sandbox Code Playgroud) 我是新来的放心和Java,我正在尝试做一个非常基本的测试,检查响应是200 ok的API.请问您能告诉我在下面的脚本中需要更改什么才能传递多个标题ID,Key和ConId?
import org.junit.Test;
import com.jayway.restassured.*;
import com.jayway.restassured.http.ContentType;
import static org.hamcrest.Matchers.*;
import static com.jayway.restassured.RestAssured.*;
public class APIresponse
{
public static void main(String[] args)
{
APIresponse apiresponse = new APIresponse();
apiresponse.response();
}
@Test
public void response ()
{
baseURI="http://testme/api/";
given().
header("Id", "abc").
param("Key", "NuDVhdsfYmNkDLOZQ").
param("ConId", "xyz").
when().
get("/uk?Id=DT44FR100731").
then().
contentType(ContentType.JSON).
body("response.code", equalTo("200"));
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Rest Assured测试API.当我检查double值时有一个AssertionError.
检查双精度的代码:
given().body(getTest()).contentType("application/json\r\n").
when()
.port(port)
.basePath("/fff/test")
.post("insert")
.then()
.assertThat()
.statusCode(200)
.body("versie", equalTo(11.0));
Run Code Online (Sandbox Code Playgroud)
这是输出:
java.lang.AssertionError: 1 expectation failed.
JSON path versie doesn't match.
Expected: <11.0>
Actual: 11.0
Run Code Online (Sandbox Code Playgroud)
当我用.body更改行时:
.body("versie", equalTo(""+11.0));
Run Code Online (Sandbox Code Playgroud)
输出是:
java.lang.AssertionError: 1 expectation failed.
JSON path versie doesn't match.
Expected: 11.0
Actual: 11.0
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么解决这个问题?因为我真的不知道如何解决这个问题.
编辑
JSON:
{
"id": 1,
"naam": "Test X",
"versie": 11.0
}
Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法记录由restassured处理的所有调用和响应(url + payload).
最终将是一个"调试"测试日志,记录所有呼叫和流量.
当然,我可以在我自己的代码中向记录器发出调用,但我首先要全局设置这种行为,而不是在我的所有测试方法中添加记录器调用.
谢谢你的任何指示
我发现了许多与此相关的类似问题......但不是我要找的具体答案.实际上我的要求没什么不同.所以张贴这个.
我想自动化Rest API,我有两个相同的选项.第一个是Rest Assured,第二个是Play框架.
对于exa.测试这个RestAPI,
http://servername:9000/dbs/all/list/m1/p1/sch1
Run Code Online (Sandbox Code Playgroud)
(这给出了xml响应)我用Java保证编写了一个代码,并且工作正常.我将它与Maven项目集成在一起,以便与Jenkins整合.示例代码:
import com.jayway.restassured
public class TestNGSimpleTest2 {
@Test
public void testApi() {
expect().
statusCode(200).
body("Status", equalTo("Su22ccess")).
when().
get("http://localhost:9000/dbs/all/list/m1/p1/sch1");
}
Run Code Online (Sandbox Code Playgroud)
所以我的第一个问题是:1.放心使用是最好的工具吗?2. Play框架更好吗?3.我发现了许多其他工具,如Jmeter,RightAPI等来测试RestAPI.但我不认为这是自动化的.我对吗?
我正在使用RestAssured来触发API调用,并且想要在REST调用失败时实现重试逻辑。
要求是因为我们会得到某些API的间歇性500响应代码,这些API通常在重试后才起作用。
我已经实现了以下代码,但它似乎不起作用-
HttpClientConfig.HttpClientFactory htc = new HttpClientConfig.HttpClientFactory() {
@Override
public HttpClient createHttpClient() {
DefaultHttpClient d = new DefaultHttpClient();
d.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(restRetryCount, true));
return d;
}
};
RestAssuredConfig rac = RestAssured.config().httpClient(HttpClientConfig.httpClientConfig().httpClientFactory(htc));
Run Code Online (Sandbox Code Playgroud)
而且,由于RestAssured不能满足HTTPClient级别的日志记录,因此我尝试使用java.util.logging启用apache httpclient的日志记录,但是这似乎也不起作用。
使用-创建了一个custom.logging.properties文件
# Enables the header wire and context logging
org.apache.http.level = FINEST
org.apache.http.wire.level = SEVERE
# Enables the context logging for connection management & request execution
org.apache.http.impl.conn.level = FINEST
org.apache.http.impl.client.level = FINEST
org.apache.http.client.level = FINEST
Run Code Online (Sandbox Code Playgroud)
并写下了以下内容以启动日志记录-
public static Logger initiateHttpTrafficLogging(){
Logger log = Logger.getLogger("httptraffic.log"); …Run Code Online (Sandbox Code Playgroud) rest-assured ×10
java ×8
rest ×5
junit ×3
json ×2
api ×1
certificate ×1
http ×1
http-headers ×1
http-post ×1
jenkins ×1
logging ×1
maven ×1
spring-boot ×1