0312 java interface test rest-assured

Keywords: Java REST JSON Dubbo


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:


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!


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.


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()
                .userName("Li Fuchun")
        String appSecret = "91e47f584dae551170ade272b2c7a69f";
        loginRestReq.setChecksum(SignUtils.generateCheckSum(loginRestReq.getAppId(), appSecret, loginRestReq.getTimestamp(), loginRestReq.getNonce()));


        ExpectModel expectModel = new ExpectModel();


RestAssured.baseURI = "https://rest-beta.xxx.com";
    final ValidatableResponse validatableResponse = given().contentType(ContentType.JSON)
                .header("requestId", UUID.randomUUID().toString())

    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.


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