lead

GetInteger () and getInteger () two methods are com. Alibaba. Fastjson. The two methods in the JSONObject, function similar to obtain from a JSONObject object to the value of a key value, The difference is that getInteger() returns an object of type INTEGER, while getInteger() returns an int, which belongs to the underlying data type.

On this basis…

origin

When I design project framework defined in standard project base class interface has a very important method of com. Funtester. Base. Interfaces. IBase# isRight, the function of the method is to validate response results conformed to the specification as well as the correctness, The business return value of the response body and the response information of the response body are usually verified. This is a non-business validation of the return value of all interfaces in the project.

Error model

The specific implementation method is as follows:

    /** * Verify that the response is valid **@paramResponse Indicates the interface response@return* /
    @Override
    public boolean isRight(JSONObject response) {
        try {
            return response.getIntValue("code") = =0;
        } catch (Exception e) {
            return false; }}Copy the code
  • Of course, this is the wrong example!

BUG analysis

Within each project, there is a fixed response structure, usually a unified response structure at the company level. The outermost key in the JSON structure response of the project is composed of code, MSG and data. The logic of this method is to take the code value from the outermost layer of response and compare it to 0.

However, in some special cases, the response structure has changed and there is no code in the outermost layer. If isRight() receives a normal JSONObject, then isRight() will still return true, which is quite different from the actual situation.

BUG fix

The correct treatment is as follows:

    /** * Verify that the response is valid **@paramResponse Indicates the interface response@return* /
    @Override
    public boolean isRight(JSONObject response) {
        try {
            return response.getInteger("code") = =0;
        } catch (Exception e) {
            return false; }}Copy the code

Most of the time, the interface responds normally, and only a response error (request failure, HTTPcode error, etc.) causes the response structure to change, which is why this BUG never appears. You can add another layer of authentication here, HTTPcode authentication. PS: In the FunTester framework, HTTPcode is stored in response in the value of FunTester.

    /** * Verify that the response is valid **@paramResponse Indicates the interface response@return* /
    @Override
    public boolean isRight(JSONObject response) {
        try {
            return response.getInteger("FunTester") == HttpStatus.SC_OK && response.getInteger("code") = =0;
        } catch (Exception e) {
            return false; }}Copy the code

If we want to add a global switch to this method to control the validation results, we can also add a judgment condition. As follows:

    /** * Verify that the response is valid **@paramResponse Indicates the interface response@return* /
    @Override
    public boolean isRight(JSONObject response) {
        try {
            return Common.VERIFY && response.getInteger("FunTester") == HttpStatus.SC_OK && response.getInteger("code") = =0;
        } catch (Exception e) {
            return false; }}Copy the code

The advantage of adding common.Verify is that some business validation can be avoided in performance testing. In the process of doing functional tests and automated tests, business validation is often performed. In the process of validating code, some time-consuming operations may be involved, which should be avoided in performance testing.

True knowledge comes from practice

Demo1

Test code in normal use:

    public static void main(String[] args) throws IOException {
        JSONObject json = getJson("code=0");
        output(json.getInteger("code") = =0);
        output(json.getIntValue("code") = =0);
    }

Copy the code

Console output:

INFO - > the current user: oker, working directory: / Users/oker/IdeaProjects funtester/system coding format: utf-88, System Mac OS X version:10.16
INFO-> true
INFO-> true

Process finished with exit code 0

Copy the code

You can’t tell the difference here. College # # Demo2

Test code when a BUG occurs:

    public static void main(String[] args) throws IOException {
        JSONObject json = getJson("code=0");
        output(json.getIntValue("co") = =0);
        output(null == json.getInteger("co"));
    }
Copy the code

Console output:

INFO - > the current user: oker, working directory: / Users/oker/IdeaProjects funtester/system coding format: utf-88, System Mac OS X version:10.16
INFO-> true
INFO-> true

Process finished with exit code 0

Copy the code

Have Fun ~ Tester!

FunTester“, a group of interesting souls, Tencent Cloud &Boss certified author, GDevOps official partner media.


  • A preliminary study of the FunTester test framework architecture diagram
  • The ultimate showdown between K6, Gatling and FunTester!
  • Single-player 120,000 QPS — FunTester revenge
  • FunTester’s past and present life
  • Automate testing in production environment
  • Tips for writing test cases
  • 7 skills to Automate tests
  • Iot testing
  • Why do tests miss bugs
  • Selenium Automation Best Practice Tips (1)
  • Selenium Automation Best Practice Tips (Middle)
  • Selenium Automation Best Practice Tips (Part 2)
  • Asynchronous authentication of Socket interfaces
  • After Selenium 4, no longer meet the API