Operations 6 min read

Business Validation Demo for Like/Unlike API in Performance Testing

The article demonstrates how to perform business‑level validation of a resource's like/unlike API during performance testing, including Java methods for the operations, a Groovy load‑testing script, and explanations of error handling within the testing framework.

FunTester
FunTester
FunTester
Business Validation Demo for Like/Unlike API in Performance Testing

During performance testing, merely checking HTTP status codes and business response codes is insufficient because an interface may still return error information; therefore, business‑level validation of the API response is crucial. This article presents a demo that validates the business logic of a resource‑related API.

The target API returns a field indicating the user's operation status on the resource, where the operation types are: 1 – like, 2 – dislike, 3 – cancel like, 4 – cancel dislike.

The load test requires data from two interfaces: a login interface to obtain authentication tokens and the resource operation interface that performs the like/dislike actions.

Previously described project structure already handles request methods and authentication, so the focus here is on the Java methods for liking/disliking and fetching resource details, each located in separate modules.

/**
 * 点赞和踩
 *
 * @param resId     资源id
 * @param operation 操作类型(踩赞类型:1-赞,2-踩,3-取消赞,4-取消踩)
 * @return
 */
public JSONObject likeOrNot(int resId, int operation) {
    String url = FlowApi.LIKE_OR_NOT;
    JSONObject params = getParams();
    params.put("isCoursePackage", 0); // 标识是否课程包 0-否 1-是
    params.put("operation", operation);
    params.put("resId", resId);
    params.put("resType", 5); // 1-课件;2-题集;3-教学资料;4-课程包;5:题目
    JSONObject response = getPostResponse(url, params);
    output(response);
    return response;
}

/**
 * 课程包详情
 *
 * @param resid 资源ID 1030167
 * @return
 */
public JSONObject courseDetail(int resid) {
    String url = LauncherApi.COURSE_DETAIL;
    JSONObject params = getParams();
    params.put("id", resid);
    JSONObject response = getPostResponse(url, params);
    // output(response);
    return response;
}

The following load‑testing script is written in Groovy, which is convenient for server‑side debugging but differs from standard Java; users should be aware of possible compiler exceptions and may need to ignore or manually resolve missing references.

public static void main(String[] args) {
    def argsUtil = new ArgsUtil(args)
    def thread = argsUtil.getIntOrdefault(0, 2)
    def times = argsUtil.getIntOrdefault(1, 2)

    def threads = []

    thread.times {
        threads << new ThreadLimitTimesCount
(it, times) {
            def resid = 1030167
            def operation = getRandomInt(4)
            def drive

            @Override
            public void before() {
                super.before()
                def base = new OkayBase(it)
                def flow = new Flow(base)
                flow.likeOrNot(this.resid, this.operation)
                this.drive = new Launcher(base)
                sleep(1000)
            }

            @Override
            protected void doing() throws Exception {
                def response = this.drive.courseDetail(this.resid)
                int anInt = response.getJSONObject("data").getInt("like_state")
                if (anInt != 3) fail()
            }

            @Override
            protected void after() {
                // cleanup if needed
            }
        }
    }

    new Concurrent(threads).start()
    allOver();
}

In the doing() method, processing the response may throw an exception; the fail() method is a wrapper that throws a generic runtime exception to mark a response failure. The testing framework catches exceptions from doing() and records statistical data about thread execution. Readers can refer to the second edition of the performance testing framework for more details.

Disclaimer: This article was originally published on the “FunTester” public account and may not be reproduced by third parties (except Tencent Cloud).

JavaPerformance Testingload testingGroovyAPI Validation
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.