Skip to content

Commit dd427a5

Browse files
committed
Add universal API interface unit testing
1 parent 629b429 commit dd427a5

File tree

4 files changed

+75
-32
lines changed

4 files changed

+75
-32
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>io.github.json031</groupId>
66
<artifactId>mcunittests</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.0.3</version>
8+
<version>1.0.5</version>
99
<name>mcunittests</name>
1010
<description>mcunittests是SpringBoot项目的一个单元测试库,是一个在MIT许可下分发的开源项目</description>
1111
<url>https://github.com/Json031/mcunittests</url>

src/main/java/io/github/json031/App.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.github.json031;
22

33
import org.junit.jupiter.api.Assertions;
4+
import org.springframework.http.*;
45
import org.springframework.web.client.RestTemplate;
56

67
import java.time.Duration;
8+
import java.util.Map;
79

810
public class MCApiTests {
911

@@ -12,42 +14,88 @@ public class MCApiTests {
1214
/**
1315
* 通用测试方法,验证给定 API 是否能在 timeout 秒内响应
1416
*
15-
* @param url 完整的 API 地址(包括 http/https)
17+
* @param url 完整的 API 地址(包括 http/https)
18+
* @param method 请求方式(GET / POST)
19+
* @param params 请求参数(POST body 或 GET 查询参数)
20+
* @param headers 请求头(可选)
1621
* @param timeout 最大允许超时时间(秒)
17-
* @return durationMillis 响应时间(毫秒)
22+
* @param verbose 是否打印响应
23+
* @return 响应时间(毫秒)
1824
*/
19-
public long assertApiRespondsWithinTimeout(String url, long timeout, boolean verbose) {
20-
return this.assertApiRespondsWithinTimeoutMillis(url, timeout * 1000, verbose);
25+
public long assertApiRespondsWithinTimeout(String url,
26+
HttpMethod method,
27+
Map<String, Object> params,
28+
Map<String, String> headers,
29+
long timeout,
30+
boolean verbose) {
31+
return this.assertApiRespondsWithinTimeoutMillis(url, method, params, headers, timeout * 1000, verbose);
2132
}
2233

2334
/**
24-
* 通用测试方法,验证给定 API 是否能在 timeout 秒内响应
35+
* 通用测试方法,验证给定 API 是否能在 timeoutMillis 内响应。
2536
*
26-
* @param url 完整的 API 地址(包括 http/https)
27-
* @param timeoutMillis 最大允许超时时间(毫秒)
28-
* @return durationMillis 响应时间(毫秒)
37+
* @param url 完整的 API 地址(包括 http/https)
38+
* @param method 请求方式(GET / POST)
39+
* @param params 请求参数(POST body 或 GET 查询参数)
40+
* @param headers 请求头(可选)
41+
* @param timeoutMillis 超时时间(毫秒)
42+
* @param verbose 是否打印响应
43+
* @return 响应时间(毫秒)
2944
*/
30-
public long assertApiRespondsWithinTimeoutMillis(String url, long timeoutMillis, boolean verbose) {
45+
public long assertApiRespondsWithinTimeoutMillis(String url,
46+
HttpMethod method,
47+
Map<String, Object> params,
48+
Map<String, String> headers,
49+
long timeoutMillis,
50+
boolean verbose) {
3151
try {
32-
long start = System.nanoTime(); // 记录开始时间
52+
HttpHeaders httpHeaders = new HttpHeaders();
53+
if (headers != null) {
54+
headers.forEach(httpHeaders::set);
55+
}
56+
57+
HttpEntity<?> entity;
58+
String finalUrl = url;
59+
60+
if (method == HttpMethod.GET && params != null && !params.isEmpty()) {
61+
// 拼接 GET 参数
62+
StringBuilder queryBuilder = new StringBuilder(url);
63+
queryBuilder.append(url.contains("?") ? "&" : "?");
64+
params.forEach((key, value) -> queryBuilder.append(key).append("=").append(value).append("&"));
65+
finalUrl = queryBuilder.substring(0, queryBuilder.length() - 1); // 去掉最后一个 &
66+
entity = new HttpEntity<>(httpHeaders);
67+
} else {
68+
// POST 请求体(可为 null)
69+
entity = new HttpEntity<>(params, httpHeaders);
70+
}
3371

34-
String response = this.restTemplate.getForObject(url, String.class);
72+
long start = System.nanoTime();
3573

36-
long end = System.nanoTime(); // 记录结束时间
74+
ResponseEntity<String> response = restTemplate.exchange(
75+
finalUrl,
76+
method,
77+
entity,
78+
String.class
79+
);
80+
81+
long end = System.nanoTime();
3782
long durationMillis = (end - start) / 1_000_000;
83+
3884
if (verbose) {
39-
System.out.println("API Response: " + response);
85+
System.out.println("API Response: " + response.getBody());
4086
System.out.println("Response time: " + durationMillis + " ms");
4187
}
4288

43-
if (durationMillis >= timeoutMillis) {
44-
Assertions.fail("API did not respond within " + timeoutMillis + " milliseconds, url: " +url);
89+
if (durationMillis > timeoutMillis) {
90+
Assertions.fail("API did not respond within " + timeoutMillis + " ms, url: " + finalUrl);
4591
}
4692

4793
return durationMillis;
94+
4895
} catch (Exception e) {
49-
Assertions.fail("Connection refused when trying to access: " + url);
96+
Assertions.fail("API call failed for: " + url + " with error: " + e.getMessage());
5097
}
98+
5199
return -1;
52100
}
53101
}

src/main/java/io/github/json031/MCHighConcurrencyTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import io.github.json031.JavaBean.HighConcurrencyResult;
44
import org.junit.jupiter.api.Assertions;
5+
import org.springframework.http.HttpMethod;
56
import org.springframework.web.client.RestTemplate;
67

78
import java.util.ArrayList;
89
import java.util.List;
10+
import java.util.Map;
911
import java.util.concurrent.*;
1012
import java.util.concurrent.atomic.AtomicInteger;
1113
import java.util.function.Supplier;
@@ -21,7 +23,13 @@ public class MCHighConcurrencyTests {
2123
* @param threadCount 并发线程数
2224
* @return 统计结果
2325
*/
24-
public HighConcurrencyResult highConcurrencyTest(String url, int threadCount, long timeoutMillis, boolean verbose) {
26+
public HighConcurrencyResult highConcurrencyTest(String url,
27+
int threadCount,
28+
HttpMethod method,
29+
Map<String, Object> params,
30+
Map<String, String> headers,
31+
long timeoutMillis,
32+
boolean verbose) {
2533
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
2634
List<Future<Long>> futures = new ArrayList<>();
2735
AtomicInteger successCount = new AtomicInteger();
@@ -31,7 +39,7 @@ public HighConcurrencyResult highConcurrencyTest(String url, int threadCount, lo
3139
futures.add(executor.submit(() -> {
3240
long start = System.nanoTime();
3341
try {
34-
long rsptime = this.mcApiTests.assertApiRespondsWithinTimeoutMillis(url, timeoutMillis, verbose); // 执行请求
42+
long rsptime = this.mcApiTests.assertApiRespondsWithinTimeoutMillis(url, method, params, headers, timeoutMillis, verbose); // 执行请求
3543
System.out.println("API Response time: " + rsptime);
3644
if (rsptime >= timeoutMillis) {
3745
failCount.incrementAndGet();

0 commit comments

Comments
 (0)