Seamher | 0140612 | 2025-06-08 19:44:36 +0800 | [diff] [blame] | 1 | package com.g9.g9backend.controller; |
| 2 | |
| 3 | import com.g9.g9backend.service.ResourceService; |
| 4 | import com.g9.g9backend.service.ThreadService; |
| 5 | import com.g9.g9backend.service.UserPurchaseService; |
| 6 | import com.g9.g9backend.service.UserUploadService; |
| 7 | import org.junit.jupiter.api.Test; |
| 8 | import org.mockito.InjectMocks; |
| 9 | import org.mockito.Mock; |
| 10 | |
| 11 | import static org.junit.jupiter.api.Assertions.*; |
| 12 | import static org.mockito.Mockito.*; |
| 13 | |
| 14 | import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | import org.mockito.junit.jupiter.MockitoExtension; |
| 16 | import org.springframework.http.ResponseEntity; |
| 17 | |
| 18 | @ExtendWith(MockitoExtension.class) |
| 19 | public class TotalControllerTest { |
| 20 | |
| 21 | @Mock |
| 22 | private ThreadService threadService; |
| 23 | |
| 24 | @Mock |
| 25 | private UserPurchaseService userPurchaseService; |
| 26 | |
| 27 | @Mock |
| 28 | private UserUploadService userUploadService; |
| 29 | |
| 30 | @Mock |
| 31 | private ResourceService resourceService; |
| 32 | |
| 33 | @InjectMocks |
| 34 | private TotalController totalController; |
| 35 | |
| 36 | @Test |
| 37 | void testGetTotalInfo() { |
| 38 | when(threadService.count()).thenReturn(10L); |
| 39 | when(userPurchaseService.count()).thenReturn(20L); |
| 40 | when(userUploadService.count()).thenReturn(5L); |
| 41 | when(resourceService.count()).thenReturn(50L); |
| 42 | |
| 43 | ResponseEntity<TotalController.Info> response = totalController.getTotalInfo(); |
| 44 | |
| 45 | assertEquals(200, response.getStatusCode().value()); |
| 46 | TotalController.Info info = response.getBody(); |
| 47 | assertNotNull(info); |
| 48 | assertEquals(10L, info.getThreadCount()); |
| 49 | assertEquals(20L, info.getDownloadCount()); |
| 50 | assertEquals(5L, info.getAuthorCount()); |
| 51 | assertEquals(50L, info.getResourceCount()); |
| 52 | } |
| 53 | } |