mcunittests是一个Maven开源项目,专注于针对Java后端项目进行自动化单元测试,支持API接口、API高并发及业务数据的自动化单元测试。
mcunittests is an open-source Maven project focused on automated unit testing for Java backend projects. It supports automated unit testing for API endpoints, high-concurrency API scenarios, and business data validation.
- 方式1️⃣通过maven安装 Install through Maven
<dependency>
<groupId>io.github.json031</groupId>
<artifactId>mcunittests</artifactId>
<version>1.0.31</version>
<scope>test</scope>
</dependency>
方式2️⃣通过GitHub Packages安装 Install through GitHub Packages
- 配置 Maven 仓库,在项目的
pom.xml
文件的repositories中,添加 GitHub Packages 仓库配置:
Configure the Maven repository and add the GitHub Packages repository configuration in the repositories of the project's pom. xml file
<repository>
<id>github</id>
<name>GitHub Json031 Apache Maven Packages</name>
<url>https://maven.pkg.github.com/json031/mcunittests</url>
</repository>
- 添加依赖 Add Dependency
在 pom.xml 中添加以下依赖:
Add the following dependencies in pom.xml:
<dependency>
<groupId>io.github.json031</groupId>
<artifactId>mcunittests</artifactId>
<version>1.0.31</version>
</dependency>
- 配置认证 Configuration authentication
在 ~/.m2/settings.xml 的servers中,配置 GitHub 的认证信息:
In ~/.m2/settings.xml servers tag, configure GitHub authentication information:
<server>
<id>github</id>
<username>your-github-username</username>
<password>your-github-TOKEN</password>
</server>
import io.github.json031.apitests.MCApiTests;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class YourProjectApplicationTests {
private MCApiTests mcApiTests = new MCApiTests();
@Test
public void testExampleApi() {
// 测试api接口地址 Replace with actual path
String apiUrl = "http://localhost:8088/json031/c/2a-d7b4-8005-a16f-8a95f07011df";
Map<String, Object> param = new HashMap<>();
param.put("id", 2);
//期望最慢响应时间 Expected slowest response time
long timeoutSeconds = 5;
mcApiTests.assertApiRespondsWithinTimeout(apiUrl, HttpMethod.GET, param, null, timeoutSeconds, true);
}
@Test
public void testApiWithHighConcurrency() {
String apiUrl = "http://localhost:8088/json031/c/2a-d7b4-8005-a16f-8a95f07011df"; // 替换为实际路径
Map<String, Object> param = new HashMap<>();
param.put("id", 2);
long timeoutSecondsMillis = 1000;
int threadCount = 1000;
HighConcurrencyResult highConcurrencyResult = this.mcHighConcurrencyTests.highConcurrencyTestWithTimeoutMillis(apiUrl, threadCount, HttpMethod.GET, param, null, timeoutSecondsMillis, true);
System.out.print("highConcurrencyResult:" + highConcurrencyResult.toString());
}
@Test
public void testIsApiValidJson() {
String apiUrl = "http://localhost:8088/json031/c/2a-d7b4-8005-a16f-8a95f07011df"; // 替换为实际路径
Map<String, Object> param = new HashMap<>();
param.put("id", 2);
this.mcApiTests.testApiReturnsValidJson(apiUrl, HttpMethod.GET, param, null, true);
}
}
This library is licensed under the MIT License.