0312 java interface test rest-assured

background

java programmers generally write that back-end services are JavaWeb-type projects, mainly including Http interface and dubbo interface, Http interface generally uses the resting style, so how to quickly test the resting interface on a third-party testing framework? The rest-assured framework is a good tool.Like a soldier's triangular spike, as shown above. Before you use it, familiarize yourself with the most basic usage methods. After writing out test cases for several interfaces, you are better able to use your prism to make basic attacks.

Let's have a hello world!<br /> <br />Suppose you have written an interface: lotto, the access path is: http://localhost:8080/lotto<br /> <br />The interface return value is:

{
"lotto":{
 "lottoId":5,
 "winning-numbers":[2,45,34,23,7,5,3],
 "winners":[{
   "winnerId":23,
   "numbers":[2,45,34,23,3,5]
 },{
   "winnerId":54,
   "numbers":[52,3,12,11,18,22]
 }]
}
}

How can I quickly verify that the interface returns to normal values?

get("/lotto").then().body("lotto.winners.winnerId", hasItems(23, 54));

Easy to use!

Introduce

Not to mention, direct maven was introduced: Note that I introduced directly from the default scope, not test; <br />mainly introduced the following two dependencies for the following reasons: <br />rest-assured: mainly testing the resting style interface of basic http, which is the most basic dependency; <br />json-path:The mainstream interface mainly returns json, tests the interface with test cases, and judges that JSON returns data in a path; <br />

<dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>rest-assured</artifactId>
      <version>4.2.0</version>
</dependency>
<dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>json-path</artifactId>
      <version>4.2.0</version>
</dependency>

You can then happily write test cases and use rest-assured for interface testing.

Key usage points

<br />Let's start with simple code!<br /> <br />Prepare test data first:

 final TestCaseDataModel<LoginRestReq> testCaseDataModel = new TestCaseDataModel<>();

        final LoginRestReq loginRestReq = LoginRestReq.builder()
                .appId("2a6bf452219cfe44c7f78231e3c80a13072b6727")
                .nonce("123456")
                .timestamp(System.currentTimeMillis())
                .userId("lxlifuchun")
                .userName("Li Fuchun")
                .build();
        String appSecret = "91e47f584dae551170ade272b2c7a69f";
        loginRestReq.setChecksum(SignUtils.generateCheckSum(loginRestReq.getAppId(), appSecret, loginRestReq.getTimestamp(), loginRestReq.getNonce()));

        testCaseDataModel.setInputParam(loginRestReq);


        ExpectModel expectModel = new ExpectModel();
        expectModel.setPath("data.id");
        expectModel.setMatcher(Matchers.lessThan(0));

        testCaseDataModel.setExpectResult(Arrays.asList(expectModel));
 	

RestAssured.baseURI = "https://rest-beta.xxx.com";
    final ValidatableResponse validatableResponse = given().contentType(ContentType.JSON)
                .header("requestId", UUID.randomUUID().toString())
                .body(testCaseData.getInputParam()).
                        post("/user_service/user/login")
                .then().contentType(ContentType.JSON);


    for (Object obj : testCaseData.getExpectResult()) {
            ExpectModel item = (ExpectModel) obj;
            validatableResponse.body(item.getPath(), item.getMatcher());
        }

The simple thing to do is to take a login interface and actually try it out:

The login interface accepts a json parameter, which LoginRestReq gets after the down-conversion; <br /> then returns the data, with a user ID in the data and a data.id path. If the ID is greater than 0, the identity login operation is successful and the login interface is normal.

Very good completion of the interface testing, if failed, will throw errors, capture errors, and then output information, identify test cases failed, prompt to the interface or send mail to the developer, that is, completed the automated testing of the interface.

Summary

It is a very useful tool to simply use the rest-assured tool to complete the interface test.Recently, I have been too busy to output a rich article. Send a short article!Hope you can help!<b

The original is not easy to reproduce. Please indicate the source.

Tags: Programming REST JSON Java Dubbo

Posted on Thu, 12 Mar 2020 09:53:30 -0700 by sfhc