0312 java interface test rest-assured


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:


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 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 />


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()
                .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 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.


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