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.
Latest Version: 1.0.31
Add the following dependency to your pom.xml:
<dependency>
<groupId>io.github.json031</groupId>
<artifactId>mcunittests</artifactId>
<version>1.0.31</version>
<scope>test</scope>
</dependency>Method 2: Install via GitHub Packages
Step 1: Configure Maven Repository
Add the GitHub Packages repository configuration in the repositories section of your project's pom.xml:
<repository>
<id>github</id>
<name>GitHub Json031 Apache Maven Packages</name>
<url>https://maven.pkg.github.com/json031/mcunittests</url>
</repository>Step 2: Add Dependency
Add the following dependency in pom.xml:
<dependency>
<groupId>io.github.json031</groupId>
<artifactId>mcunittests</artifactId>
<version>1.0.31</version>
</dependency>Step 3: Configure Authentication
Configure GitHub authentication information in the servers section of ~/.m2/settings.xml:
<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;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class YourProjectApplicationTests {
private MCApiTests mcApiTests = new MCApiTests();
@Test
public void testExampleApi() {
// Replace with actual API endpoint
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 (in seconds)
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.
mcunittests 是一个 Maven 开源项目,专注于针对 Java 后端项目进行自动化单元测试,支持 API 接口、API 高并发及业务数据的自动化单元测试。
最新版本: 1.0.31
在 pom.xml 中添加以下依赖:
<dependency>
<groupId>io.github.json031</groupId>
<artifactId>mcunittests</artifactId>
<version>1.0.31</version>
<scope>test</scope>
</dependency>方式 2:通过 GitHub Packages 安装
步骤 1:配置 Maven 仓库
在项目的 pom.xml 文件的 repositories 中,添加 GitHub Packages 仓库配置:
<repository>
<id>github</id>
<name>GitHub Json031 Apache Maven Packages</name>
<url>https://maven.pkg.github.com/json031/mcunittests</url>
</repository>步骤 2:添加依赖
在 pom.xml 中添加以下依赖:
<dependency>
<groupId>io.github.json031</groupId>
<artifactId>mcunittests</artifactId>
<version>1.0.31</version>
</dependency>步骤 3:配置认证
在 ~/.m2/settings.xml 的 servers 中,配置 GitHub 的认证信息:
<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;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class YourProjectApplicationTests {
private MCApiTests mcApiTests = new MCApiTests();
@Test
public void testExampleApi() {
// 测试 API 接口地址
String apiUrl = "http://localhost:8088/json031/c/2a-d7b4-8005-a16f-8a95f07011df";
Map<String, Object> param = new HashMap<>();
param.put("id", 2);
// 期望最慢响应时间(秒)
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);
}本项目基于 MIT License 开源协议。
