0312 java interface test rest-assured

Keywords: Java REST JSON Dubbo

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!


Suppose you have written an interface: lotto, the access path is: http://localhost:8080/lotto


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 by default scope, not test;
The following two dependencies were introduced for the following reasons:
rest-assured: mainly tests the basic http resting-style interface, which is the most basic dependency;
json-path: The mainstream interface mainly returns json, tests the interface with test cases, and judges that JSON returns data under a path.

<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


Let's start with simple code!


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 is obtained after the LoginRestReq pair is down-converted;
Then return the data, there is a user ID in the data, the path is data.id, if the ID is greater than 0, the logon operation is successful, and the logon 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!The original is not easy to reproduce. Please indicate the source.

Posted by bugsuperstar37 on Thu, 12 Mar 2020 10:05:16 -0700