用户类 邀请码 商城
Change-Id: If3a9ef5c464386647ae3b876104cbf4c71265c4b
diff --git a/src/test/java/com/pt5/pthouduan/ControllerTest/InvitesControllerTest.java b/src/test/java/com/pt5/pthouduan/ControllerTest/InvitesControllerTest.java
index 9477df4..345d6a2 100644
--- a/src/test/java/com/pt5/pthouduan/ControllerTest/InvitesControllerTest.java
+++ b/src/test/java/com/pt5/pthouduan/ControllerTest/InvitesControllerTest.java
@@ -1,5 +1,6 @@
package com.pt5.pthouduan.ControllerTest;
+
import com.pt5.pthouduan.controller.InvitesController;
import com.pt5.pthouduan.service.InviteService;
import org.junit.jupiter.api.BeforeEach;
@@ -29,70 +30,96 @@
}
@Test
- void soldinvite_ShouldCallServiceWithCorrectParameter() {
+ void getUserInfo_Success() {
// 准备测试数据
- String testBuyerName = "testBuyer";
Map<String, Object> expectedResponse = new HashMap<>();
expectedResponse.put("success", true);
- expectedResponse.put("message", "Invite sold successfully");
+ expectedResponse.put("invites", 5);
+ expectedResponse.put("username", "testuser");
- // 模拟服务行为
- when(inviteService.setbuyername(anyString())).thenReturn(expectedResponse);
+ // 模拟服务层行为
+ when(inviteService.getInvitesByUsername(anyString()))
+ .thenReturn(expectedResponse);
// 执行测试
- Map<String, Object> actualResponse = invitesController.soldinvite(testBuyerName);
-
- // 验证行为
- verify(inviteService, times(1)).setbuyername(testBuyerName);
+ Map<String, Object> actualResponse = invitesController.getUserInfo("testuser");
// 验证结果
assertEquals(expectedResponse, actualResponse);
- assertTrue((Boolean) actualResponse.get("success"));
- assertEquals("Invite sold successfully", actualResponse.get("message"));
+ verify(inviteService, times(1)).getInvitesByUsername("testuser");
}
@Test
- void soldinvite_ShouldHandleServiceFailure() {
- // 准备测试数据
- String testBuyerName = "testBuyer";
+ void getUserInfo_UserNotFound() {
Map<String, Object> expectedResponse = new HashMap<>();
expectedResponse.put("success", false);
- expectedResponse.put("error", "Invalid buyer name");
+ expectedResponse.put("message", "用户不存在");
- // 模拟服务行为
- when(inviteService.setbuyername(anyString())).thenReturn(expectedResponse);
+ when(inviteService.getInvitesByUsername(anyString()))
+ .thenReturn(expectedResponse);
- // 执行测试
- Map<String, Object> actualResponse = invitesController.soldinvite(testBuyerName);
+ Map<String, Object> actualResponse = invitesController.getUserInfo("nonexistent");
- // 验证行为
- verify(inviteService, times(1)).setbuyername(testBuyerName);
-
- // 验证结果
assertEquals(expectedResponse, actualResponse);
assertFalse((Boolean) actualResponse.get("success"));
- assertEquals("Invalid buyer name", actualResponse.get("error"));
}
@Test
- void soldinvite_ShouldHandleNullInput() {
- // 准备测试数据
+ void getUserInfo_EmptyUsername() {
Map<String, Object> expectedResponse = new HashMap<>();
expectedResponse.put("success", false);
- expectedResponse.put("error", "Buyer name cannot be null");
+ expectedResponse.put("message", "用户名不能为空");
- // 模拟服务行为
- when(inviteService.setbuyername(null)).thenReturn(expectedResponse);
+ when(inviteService.getInvitesByUsername(""))
+ .thenReturn(expectedResponse);
- // 执行测试
- Map<String, Object> actualResponse = invitesController.soldinvite(null);
+ Map<String, Object> actualResponse = invitesController.getUserInfo("");
- // 验证行为
- verify(inviteService, times(1)).setbuyername(null);
-
- // 验证结果
assertEquals(expectedResponse, actualResponse);
- assertFalse((Boolean) actualResponse.get("success"));
- assertEquals("Buyer name cannot be null", actualResponse.get("error"));
+ assertEquals("用户名不能为空", actualResponse.get("message"));
+ }
+
+ @Test
+ void getUserInfo_ServiceException() {
+ when(inviteService.getInvitesByUsername(anyString()))
+ .thenThrow(new RuntimeException("数据库连接失败"));
+
+ assertThrows(RuntimeException.class, () -> {
+ invitesController.getUserInfo("testuser");
+ });
+ }
+
+ // 边界条件测试
+ @Test
+ void getUserInfo_LongUsername() {
+ String longUsername = "a".repeat(256);
+ Map<String, Object> expectedResponse = new HashMap<>();
+ expectedResponse.put("success", false);
+ expectedResponse.put("message", "用户名过长");
+
+ when(inviteService.getInvitesByUsername(longUsername))
+ .thenReturn(expectedResponse);
+
+ Map<String, Object> actualResponse = invitesController.getUserInfo(longUsername);
+
+ assertEquals(expectedResponse, actualResponse);
+ }
+
+ // 性能测试示例(实际项目中可能需要单独的测试类)
+ @Test
+ void getUserInfo_Performance() {
+ // 模拟快速响应
+ when(inviteService.getInvitesByUsername(anyString()))
+ .thenAnswer(invocation -> {
+ Map<String, Object> response = new HashMap<>();
+ response.put("success", true);
+ return response;
+ });
+
+ long startTime = System.currentTimeMillis();
+ invitesController.getUserInfo("testuser");
+ long duration = System.currentTimeMillis() - startTime;
+
+ assertTrue(duration < 100, "响应时间应小于100毫秒");
}
}