Skip to content

Commit e1250bd

Browse files
committed
Add unit tests
1 parent 217624a commit e1250bd

File tree

5 files changed

+162
-40
lines changed

5 files changed

+162
-40
lines changed

pom.xml

Lines changed: 9 additions & 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.26</version>
8+
<version>1.0.27</version>
99
<name>mcunittests</name>
1010
<description>mcunittests是SpringBoot项目的一个单元测试库,是一个在MIT许可下分发的开源项目</description>
1111
<url>https://github.com/Json031/mcunittests</url>
@@ -45,6 +45,14 @@
4545
<artifactId>jackson-databind</artifactId>
4646
<version>2.15.0</version>
4747
</dependency>
48+
49+
<!-- Mockito core -->
50+
<dependency>
51+
<groupId>org.mockito</groupId>
52+
<artifactId>mockito-core</artifactId>
53+
<version>5.11.0</version>
54+
<scope>test</scope>
55+
</dependency>
4856
</dependencies>
4957

5058
<licenses>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
public class MCHighConcurrencyTests {
1717

18-
private final MCApiTests mcApiTests = new MCApiTests();
18+
public MCApiTests mcApiTests = new MCApiTests();
1919

2020
/**
2121
* 运行并发测试

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

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.json031;
2+
3+
import io.github.json031.JavaBean.RequestUnitTestsResult;
4+
import io.github.json031.unittests.DataUnitTests;
5+
import io.github.json031.unittests.RequestUnitTests;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.HttpMethod;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.http.ResponseEntity;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
public class DataUnitTestsTest {
15+
16+
@Test
17+
public void testIsValidJSON() {
18+
HttpHeaders headers = new HttpHeaders();
19+
headers.setContentType(MediaType.APPLICATION_JSON);
20+
ResponseEntity<String> response = ResponseEntity.ok()
21+
.headers(headers)
22+
.body("{\"key\":\"value\"}");
23+
24+
assertTrue(DataUnitTests.isValidJSON(response));
25+
}
26+
27+
@Test
28+
public void testIsValidUrl() {
29+
DataUnitTests.isValidUrl("https://www.baidu.com");
30+
}
31+
32+
@Test
33+
public void testIsJSONContentType_ResponseEntity() {
34+
HttpHeaders headers = new HttpHeaders();
35+
headers.setContentType(MediaType.APPLICATION_JSON);
36+
ResponseEntity<String> response = ResponseEntity.ok()
37+
.headers(headers)
38+
.body("{\"key\":\"value\"}");
39+
40+
assertTrue(DataUnitTests.isJSONContentType(response));
41+
}
42+
43+
@Test
44+
public void testIsJSONContentType_MediaType() {
45+
assertTrue(DataUnitTests.isJSONContentType(MediaType.APPLICATION_JSON));
46+
assertFalse(DataUnitTests.isJSONContentType(MediaType.TEXT_PLAIN));
47+
}
48+
49+
@Test
50+
public void testRequestUnitTests() {
51+
String url = "https://www.baidu.com";
52+
// 调用请求方法
53+
RequestUnitTestsResult result = RequestUnitTests.requestWitRestTemplate(url, HttpMethod.GET, null, null, true);
54+
55+
// 验证结果
56+
assertNotNull(result);
57+
assertTrue(result.durationMillis < 2000);
58+
assertEquals(200, result.response.getStatusCode().value());
59+
}
60+
}
61+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package io.github.json031;
2+
3+
import io.github.json031.JavaBean.HighConcurrencyResult;
4+
import io.github.json031.apitests.MCHighConcurrencyTests;
5+
import io.github.json031.apitests.MCApiTests;
6+
import org.junit.jupiter.api.Test;
7+
import org.mockito.Mockito;
8+
import org.springframework.http.HttpMethod;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
import static org.junit.jupiter.api.Assertions.*;
14+
15+
public class MCHighConcurrencyTestsTest {
16+
17+
@Test
18+
public void testHighConcurrencyTestWithTimeoutSeconds() {
19+
// 准备一个假的 MCHighConcurrencyTests 对象,内部的 MCApiTests 被 mock
20+
MCHighConcurrencyTests tests = new MCHighConcurrencyTests() {
21+
@Override
22+
public HighConcurrencyResult highConcurrencyTestWithTimeoutMillis(String url, int threadCount, HttpMethod method,
23+
Map<String, Object> params, Map<String, String> headers,
24+
long timeoutMillis, boolean verbose) {
25+
return new HighConcurrencyResult(threadCount, threadCount, 0, 100); // 假设全部成功
26+
}
27+
};
28+
29+
Map<String, Object> params = new HashMap<>();
30+
params.put("key", "value");
31+
32+
HighConcurrencyResult result = tests.highConcurrencyTestWithTimeoutSeconds(
33+
"https://www.baidu.com",
34+
5,
35+
HttpMethod.GET,
36+
params,
37+
null,
38+
2, // 2秒超时
39+
false
40+
);
41+
42+
assertNotNull(result);
43+
assertEquals(5, result.total);
44+
assertEquals(5, result.success);
45+
assertEquals(0, result.failed);
46+
assertTrue(result.avgResponseTimeMillis > 0);
47+
assertTrue(result.avgResponseTimeMillis < 2000);
48+
}
49+
50+
@Test
51+
public void testHighConcurrencyTestWithTimeoutMillis_RealisticMock() {
52+
// 这里对 MCApiTests 进行 mock(为了不真的去请求网络)
53+
MCHighConcurrencyTests tests = new MCHighConcurrencyTests();
54+
MCApiTests mockApiTests = Mockito.mock(MCApiTests.class);
55+
tests.mcApiTests = mockApiTests;
56+
57+
try {
58+
// 设定 mock 返回固定的响应时间
59+
Mockito.when(mockApiTests.assertApiRespondsWithinTimeoutMillis(
60+
Mockito.anyString(),
61+
Mockito.any(HttpMethod.class),
62+
Mockito.anyMap(),
63+
Mockito.anyMap(),
64+
Mockito.anyLong(),
65+
Mockito.anyBoolean()
66+
)).thenReturn(50L); // 50ms
67+
68+
Map<String, Object> params = new HashMap<>();
69+
params.put("test", "value");
70+
71+
HighConcurrencyResult result = tests.highConcurrencyTestWithTimeoutMillis(
72+
"http://example.com/api",
73+
3,
74+
HttpMethod.GET,
75+
params,
76+
null,
77+
1000,
78+
false
79+
);
80+
81+
assertNotNull(result);
82+
assertEquals(3, result.total);
83+
assertEquals(3, result.success);
84+
assertEquals(0, result.failed);
85+
assertTrue(result.avgResponseTimeMillis > 0);
86+
assertTrue(result.avgResponseTimeMillis < 2000);
87+
} catch (Exception e) {
88+
fail("Exception thrown in highConcurrencyTestWithTimeoutMillis: " + e.getMessage());
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)