Sure233 | 8188c5d | 2025-05-28 11:43:06 +0800 | [diff] [blame^] | 1 | package com.pt5.pthouduan.ControllerTest; |
| 2 | |
| 3 | import com.pt5.pthouduan.controller.InvitesController; |
| 4 | import com.pt5.pthouduan.service.InviteService; |
| 5 | import org.junit.jupiter.api.BeforeEach; |
| 6 | import org.junit.jupiter.api.Test; |
| 7 | import org.mockito.InjectMocks; |
| 8 | import org.mockito.Mock; |
| 9 | import org.mockito.MockitoAnnotations; |
| 10 | |
| 11 | import java.util.HashMap; |
| 12 | import java.util.Map; |
| 13 | |
| 14 | import static org.junit.jupiter.api.Assertions.*; |
| 15 | import static org.mockito.ArgumentMatchers.anyString; |
| 16 | import static org.mockito.Mockito.*; |
| 17 | |
| 18 | class InvitesControllerTest { |
| 19 | |
| 20 | @Mock |
| 21 | private InviteService inviteService; |
| 22 | |
| 23 | @InjectMocks |
| 24 | private InvitesController invitesController; |
| 25 | |
| 26 | @BeforeEach |
| 27 | void setUp() { |
| 28 | MockitoAnnotations.openMocks(this); |
| 29 | } |
| 30 | |
| 31 | @Test |
| 32 | void soldinvite_ShouldCallServiceWithCorrectParameter() { |
| 33 | // 准备测试数据 |
| 34 | String testBuyerName = "testBuyer"; |
| 35 | Map<String, Object> expectedResponse = new HashMap<>(); |
| 36 | expectedResponse.put("success", true); |
| 37 | expectedResponse.put("message", "Invite sold successfully"); |
| 38 | |
| 39 | // 模拟服务行为 |
| 40 | when(inviteService.setbuyername(anyString())).thenReturn(expectedResponse); |
| 41 | |
| 42 | // 执行测试 |
| 43 | Map<String, Object> actualResponse = invitesController.soldinvite(testBuyerName); |
| 44 | |
| 45 | // 验证行为 |
| 46 | verify(inviteService, times(1)).setbuyername(testBuyerName); |
| 47 | |
| 48 | // 验证结果 |
| 49 | assertEquals(expectedResponse, actualResponse); |
| 50 | assertTrue((Boolean) actualResponse.get("success")); |
| 51 | assertEquals("Invite sold successfully", actualResponse.get("message")); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | void soldinvite_ShouldHandleServiceFailure() { |
| 56 | // 准备测试数据 |
| 57 | String testBuyerName = "testBuyer"; |
| 58 | Map<String, Object> expectedResponse = new HashMap<>(); |
| 59 | expectedResponse.put("success", false); |
| 60 | expectedResponse.put("error", "Invalid buyer name"); |
| 61 | |
| 62 | // 模拟服务行为 |
| 63 | when(inviteService.setbuyername(anyString())).thenReturn(expectedResponse); |
| 64 | |
| 65 | // 执行测试 |
| 66 | Map<String, Object> actualResponse = invitesController.soldinvite(testBuyerName); |
| 67 | |
| 68 | // 验证行为 |
| 69 | verify(inviteService, times(1)).setbuyername(testBuyerName); |
| 70 | |
| 71 | // 验证结果 |
| 72 | assertEquals(expectedResponse, actualResponse); |
| 73 | assertFalse((Boolean) actualResponse.get("success")); |
| 74 | assertEquals("Invalid buyer name", actualResponse.get("error")); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | void soldinvite_ShouldHandleNullInput() { |
| 79 | // 准备测试数据 |
| 80 | Map<String, Object> expectedResponse = new HashMap<>(); |
| 81 | expectedResponse.put("success", false); |
| 82 | expectedResponse.put("error", "Buyer name cannot be null"); |
| 83 | |
| 84 | // 模拟服务行为 |
| 85 | when(inviteService.setbuyername(null)).thenReturn(expectedResponse); |
| 86 | |
| 87 | // 执行测试 |
| 88 | Map<String, Object> actualResponse = invitesController.soldinvite(null); |
| 89 | |
| 90 | // 验证行为 |
| 91 | verify(inviteService, times(1)).setbuyername(null); |
| 92 | |
| 93 | // 验证结果 |
| 94 | assertEquals(expectedResponse, actualResponse); |
| 95 | assertFalse((Boolean) actualResponse.get("success")); |
| 96 | assertEquals("Buyer name cannot be null", actualResponse.get("error")); |
| 97 | } |
| 98 | } |