OCT REM: 1. 优化单元测试执行速度

This commit is contained in:
HuangXin 2025-03-31 15:54:14 +08:00
parent d5c2d35f84
commit 9a28f38e47
2 changed files with 36 additions and 24 deletions

View File

@ -25,7 +25,6 @@ import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
@ -38,6 +37,7 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
@ -49,7 +49,6 @@ import static org.mockito.Mockito.when;
* @version 1.0.0
* @since 2025-02-10
*/
@SpringBootTest
@DisplayName("HttpClient 测试类")
@SuppressWarnings("unchecked")
public class HttpClientUtilsTest {
@ -67,6 +66,7 @@ public class HttpClientUtilsTest {
private MockedStatic<HttpClients> httpClientsMock;
private MockedStatic<HttpAsyncClients> httpAsyncClientsMock;
private MockedStatic<EntityUtils> entityUtilsMock;
private MockedStatic<MessageUtil> messageUtilMock;
// 创建 Mock HttpClient
private CloseableHttpClient mockHttpClient;
@ -92,6 +92,7 @@ public class HttpClientUtilsTest {
httpClientsMock = mockStatic(HttpClients.class, Mockito.CALLS_REAL_METHODS);
httpAsyncClientsMock = mockStatic(HttpAsyncClients.class, Mockito.CALLS_REAL_METHODS);
entityUtilsMock = mockStatic(EntityUtils.class);
messageUtilMock = mockStatic(MessageUtil.class);
mockHttpClient = Mockito.mock(CloseableHttpClient.class);
mockResponse = Mockito.mock(ClassicHttpResponse.class);
@ -112,6 +113,7 @@ public class HttpClientUtilsTest {
httpClientsMock.when(HttpClients::createDefault).thenReturn(mockHttpClient);
httpAsyncClientsMock.when(HttpAsyncClients::createDefault).thenReturn(asyncClient);
messageUtilMock.when(() -> MessageUtil.get(anyString(), anyString())).thenReturn("Mocked message");
}
/**
@ -123,6 +125,7 @@ public class HttpClientUtilsTest {
platformMock.close();
httpAsyncClientsMock.close();
entityUtilsMock.close();
messageUtilMock.close();
}
/**
@ -160,7 +163,8 @@ public class HttpClientUtilsTest {
Mockito.when(mockResponse.getCode()).thenReturn(404);
mockResponse.setEntity(new StringEntity(FAILURE_JSON_RESPONSE, StandardCharsets.UTF_8));
// 模拟对于不同的响应处理器返回不同的值
Mockito.when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class))).thenAnswer(invocation -> handleResponse(mockResponse));
Mockito.when(mockHttpClient.execute(any(ClassicHttpRequest.class), any(HttpClientResponseHandler.class)))
.thenAnswer(invocation -> handleResponse(mockResponse));
// 调用 get 方法并验证结果
assertThrows(CommonErrorCodeException.class, () -> HttpClientUtils.get(FAILURE_URL, null, null));
}

View File

@ -1,10 +1,15 @@
package com.cmcc.magent.misc;
import com.cmcc.magent.config.ObjectMapperProvider;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import java.lang.reflect.Constructor;
@ -13,6 +18,10 @@ import java.lang.reflect.InvocationTargetException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
/**
* 测试类 {@code JsonUtilsTest} 用于验证 {@code JsonUtils} 工具类的功能是否正常工作
@ -26,15 +35,17 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
* @version 1.0.0
* @since 2025-01-15
*/
@SpringBootTest
@DisplayName("JSON辅助功能函数")
public class JsonUtilsTest {
/**
* 测试用的 {@code Person} 对象提供测试数据
*/
private Person person;
private MockedStatic<ObjectMapperProvider> providerMockedStatic;
private ObjectMapper mockObjectMapper;
/**
* 在每个测试运行之前初始化测试数据
* <p>使用 {@link BeforeEach} 注解确保每次运行测试方法前都会执行此初始化逻辑</p>
@ -44,6 +55,15 @@ public class JsonUtilsTest {
person = new Person();
person.setName("John Doe");
person.setAge(30);
MockitoAnnotations.openMocks(this);
mockObjectMapper = mock(ObjectMapper.class);
providerMockedStatic = mockStatic(ObjectMapperProvider.class);
providerMockedStatic.when(ObjectMapperProvider::getMapper).thenReturn(mockObjectMapper);
}
@AfterEach
public void tearDown() {
providerMockedStatic.close();
}
/**
@ -62,29 +82,17 @@ public class JsonUtilsTest {
@DisplayName("对象序列化")
public void getJson_ValidObject_ReturnsJsonString() throws JsonProcessingException {
String expectedJson = "{\"name\":\"John Doe\",\"age\":30}";
String actualJson = JsonUtils.getJson(person);
assertEquals(expectedJson, actualJson);
}
when(mockObjectMapper.writeValueAsString(any())).thenReturn(expectedJson);
/**
* 测试 {@code JsonUtils.getJson(Object)} 方法验证当对象存在循环引用时是否抛出异常
*
* <p>测试内容</p>
* <ul>
* <li>设置 {@code Person} 对象为自身的好友即创建循环引用</li>
* <li>调用 {@code JsonUtils.getJson(Object)} 方法并验证是否抛出 {@link JsonProcessingException} 异常</li>
* </ul>
*
* @see JsonUtils#getJson(Object)
*/
@Test
@DisplayName("对象序列化异常")
public void getJson_ObjectWithCircularReference_ThrowsJsonProcessingException() {
person.setFriend(person); // 创建循环引用
// 拦截并同步执行异步任务
String actualJson = JsonUtils.getJson(person);
assertEquals(expectedJson, actualJson);
person.setFriend(person);
when(mockObjectMapper.writeValueAsString(any())).thenThrow(new JsonProcessingException(""){});
assertThrows(JsonProcessingException.class, () -> JsonUtils.getJson(person));
}
@Test
@DisplayName("禁止类实例化")
public void testPrivateConstructor() {