Skip to content

Commit 0dc9329

Browse files
committed
New RequestUnitTestsResult
1 parent 832cb66 commit 0dc9329

File tree

6 files changed

+134
-40
lines changed

6 files changed

+134
-40
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<dependency>
1515
<groupId>io.github.json031</groupId>
1616
<artifactId>mcunittests</artifactId>
17-
<version>1.0.30</version>
17+
<version>1.0.31</version>
1818
<scope>test</scope>
1919
</dependency>
2020

@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>io.github.json031</groupId>
4444
<artifactId>mcunittests</artifactId>
45-
<version>1.0.30</version>
45+
<version>1.0.31</version>
4646
</dependency>
4747
```
4848

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.30</version>
8+
<version>1.0.31</version>
99
<name>mcunittests</name>
1010
<description>
1111
mcunittests是一个Maven开源项目,专注于针对Java后端项目进行自动化单元测试,支持API接口、API高并发及业务数据的自动化单元测试。

src/main/java/io/github/json031/JavaBean/RequestUnitTestsResult.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,81 @@
22

33
import org.springframework.http.ResponseEntity;
44

5+
import java.time.Instant;
6+
57
/**
68
* Request Unit Tests Result.
79
*/
810
public class RequestUnitTestsResult {
11+
//请求发起时间与结束时间(用于更精确分析请求波峰)
12+
public final Instant startTime;
13+
public final Instant endTime;
14+
//错误信息(Error Details)
15+
public final String errorMessage;
16+
17+
//是否请求成功(Success Boolean)
18+
public final boolean isSuccess;
19+
20+
//请求状态码(HTTP Status Code)
21+
public final int statusCode;
22+
//请求 URL(用于动态构造或多 API 测试时溯源)
23+
public final String requestUrl;
24+
//请求方法(GET / POST / PUT / DELETE 等)
25+
public final String method;
26+
//线程 ID(并发测试中识别发起线程)
27+
public final long threadId;
28+
929
// 毫秒级耗时
1030
public final long durationMillis;
1131
// api请求响应结果
1232
public final ResponseEntity<String> response;
33+
// 响应大小(字节)
34+
public final int responseSizeBytes;
1335

14-
public RequestUnitTestsResult(long durationMillis, ResponseEntity<String> response) {
36+
public RequestUnitTestsResult(
37+
long durationMillis,
38+
ResponseEntity<String> response,
39+
int statusCode,
40+
boolean isSuccess,
41+
String errorMessage,
42+
String requestUrl,
43+
String method,
44+
int responseSizeBytes,
45+
Instant startTime,
46+
Instant endTime,
47+
long threadId
48+
) {
1549
this.durationMillis = durationMillis;
1650
this.response = response;
51+
this.statusCode = statusCode;
52+
this.isSuccess = isSuccess;
53+
this.errorMessage = errorMessage;
54+
this.requestUrl = requestUrl;
55+
this.method = method;
56+
this.startTime = startTime;
57+
this.endTime = endTime;
58+
this.responseSizeBytes = responseSizeBytes;
59+
this.threadId = threadId;
60+
}
61+
62+
public static RequestUnitTestsResult testSuccessResult(
63+
long durationMillis,
64+
ResponseEntity<String> response,
65+
String requestUrl,
66+
String method
67+
) {
68+
RequestUnitTestsResult testSuccessResult = new RequestUnitTestsResult(
69+
durationMillis,
70+
response,
71+
200,
72+
true,
73+
"",
74+
requestUrl,
75+
method,
76+
0,
77+
Instant.now(),
78+
Instant.now(),
79+
0);
80+
return testSuccessResult;
1781
}
1882
}

src/main/java/io/github/json031/unittests/RequestUnitTests.java

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.springframework.http.ResponseEntity;
99
import org.springframework.web.client.RestTemplate;
1010

11+
import java.nio.charset.StandardCharsets;
12+
import java.time.Instant;
1113
import java.util.Map;
1214

1315
/**
@@ -56,48 +58,76 @@ public static RequestUnitTestsResult requestWitRestTemplate(String url,
5658
boolean verbose) {
5759
//校验 URL 是否合法
5860
if (DataUnitTests.isValidUrl(url)) {
59-
try {
60-
HttpHeaders httpHeaders = new HttpHeaders();
61-
if (headers != null) {
62-
headers.forEach(httpHeaders::set);
63-
}
61+
HttpHeaders httpHeaders = new HttpHeaders();
62+
if (headers != null) {
63+
headers.forEach(httpHeaders::set);
64+
}
6465

65-
// 请求体
66-
HttpEntity<?> entity;
67-
// 请求地址
68-
String finalUrl = url;
69-
70-
if (method == HttpMethod.GET && params != null && !params.isEmpty()) {
71-
// 拼接 GET 参数
72-
StringBuilder queryBuilder = new StringBuilder(url);
73-
queryBuilder.append(url.contains("?") ? "&" : "?");
74-
params.forEach((key, value) -> queryBuilder.append(key).append("=").append(value).append("&"));
75-
finalUrl = queryBuilder.substring(0, queryBuilder.length() - 1); // 去掉最后一个 &
76-
entity = new HttpEntity<>(httpHeaders);
77-
} else {
78-
// POST 请求体(可为 null)
79-
entity = new HttpEntity<>(params, httpHeaders);
80-
}
66+
// 请求体
67+
HttpEntity<?> entity;
68+
// 请求地址
69+
String finalUrl = url;
70+
71+
if (method == HttpMethod.GET && params != null && !params.isEmpty()) {
72+
// 拼接 GET 参数
73+
StringBuilder queryBuilder = new StringBuilder(url);
74+
queryBuilder.append(url.contains("?") ? "&" : "?");
75+
params.forEach((key, value) -> queryBuilder.append(key).append("=").append(value).append("&"));
76+
finalUrl = queryBuilder.substring(0, queryBuilder.length() - 1); // 去掉最后一个 &
77+
entity = new HttpEntity<>(httpHeaders);
78+
} else {
79+
// POST 请求体(可为 null)
80+
entity = new HttpEntity<>(params, httpHeaders);
81+
}
8182

82-
long start = System.nanoTime();
83+
Instant startTime = Instant.now();
84+
long startNano = System.nanoTime();
8385

84-
ResponseEntity<String> response = INSTANCE.restTemplate.exchange(finalUrl, method, entity, String.class);
86+
ResponseEntity<String> response = null;
87+
String errorMessage = null;
88+
int statusCode = 0;
89+
boolean isSuccess = false;
90+
int responseSizeBytes = 0;
91+
String methodStr = method.name();
92+
long threadId = Thread.currentThread().getId();
8593

86-
long end = System.nanoTime();
87-
// 毫秒级耗时
88-
long durationMillis = (end - start) / 1_000_000;
94+
try {
95+
response = INSTANCE.restTemplate.exchange(finalUrl, method, entity, String.class);
96+
97+
if (response != null) {
98+
statusCode = response.getStatusCodeValue();
99+
isSuccess = response.getStatusCode().is2xxSuccessful();
100+
if (response.getBody() != null) {
101+
responseSizeBytes = response.getBody().getBytes(StandardCharsets.UTF_8).length;
102+
}
103+
}
89104

90105
if (verbose) {
91-
System.out.println("API Response: " + response.getBody());
92-
System.out.println("Response time: " + durationMillis + " ms");
106+
System.out.println("API Response: " + (response != null ? response.getBody() : "null"));
93107
}
94108

95-
RequestUnitTestsResult requestUnitTestsResult = new RequestUnitTestsResult(durationMillis, response);
96-
return requestUnitTestsResult;
97-
98109
} catch (Exception e) {
99-
System.out.println("API call failed for: " + url + " with error: " + e.getMessage());
110+
errorMessage = e.getMessage();
111+
System.out.println("API call failed for: " + url + " with error: " + errorMessage);
100112
}
113+
114+
long endNano = System.nanoTime();
115+
long durationMillis = (endNano - startNano) / 1_000_000;
116+
Instant endTime = Instant.now();
117+
118+
return new RequestUnitTestsResult(
119+
durationMillis,
120+
response,
121+
statusCode,
122+
isSuccess,
123+
errorMessage,
124+
finalUrl,
125+
methodStr,
126+
responseSizeBytes,
127+
startTime,
128+
endTime,
129+
threadId
130+
);
101131
}
102132

103133
return null;

src/test/java/io/github/json031/DataUnitTestsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void testIsJSONContentType_ResponseEntity() {
3838
ResponseEntity<String> response = ResponseEntity.ok()
3939
.headers(headers)
4040
.body("{\"key\":\"value\"}");
41-
RequestUnitTestsResult result = new RequestUnitTestsResult(0, response);
41+
RequestUnitTestsResult result = RequestUnitTestsResult.testSuccessResult(0, response, "","GET");
4242

4343
assertTrue(DataUnitTests.isJSONContentType(response));
4444
assertTrue(DataUnitTests.isJSONContentType(result));

src/test/java/io/github/json031/FailUnitTestsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public void testRequestUnitTests() {
4242
HttpMethod method = HttpMethod.GET;
4343
// 这个端口很可能没有服务运行,会连接失败从而抛出异常
4444
Map<String, Object> param2 = new HashMap<>();
45-
Assertions.assertNull(
46-
RequestUnitTests.requestWitRestTemplate(fakeUrl, method, param2, null, true)
45+
Assertions.assertFalse(
46+
RequestUnitTests.requestWitRestTemplate(fakeUrl, method, param2, null, true).isSuccess
4747
);
4848
}
4949

@@ -80,7 +80,7 @@ public void testDataUnitTests() {
8080
ResponseEntity<String> response = ResponseEntity.ok()
8181
.headers(headers)
8282
.body("key}");
83-
RequestUnitTestsResult result = new RequestUnitTestsResult(10, response);
83+
RequestUnitTestsResult result = RequestUnitTestsResult.testSuccessResult(10, response, "","GET");
8484
assertFalse(DataUnitTests.isValidJSON(response));
8585
assertFalse(DataUnitTests.withinTimeOut(result, 0));
8686
assertFalse(DataUnitTests.isValidUrl("s"));

0 commit comments

Comments
 (0)