夜雨声烦 | 451d71c | 2025-05-20 00:58:36 +0800 | [diff] [blame] | 1 | package com.example.g8backend.service; |
| 2 | |
| 3 | import com.example.g8backend.entity.User; |
| 4 | import com.example.g8backend.mapper.UserMapper; |
| 5 | import com.example.g8backend.service.impl.AdminServiceImpl; |
| 6 | import org.junit.jupiter.api.Test; |
| 7 | import org.junit.jupiter.api.extension.ExtendWith; |
| 8 | import org.mockito.InjectMocks; |
| 9 | import org.mockito.Mock; |
| 10 | import org.mockito.junit.jupiter.MockitoExtension; |
| 11 | import static org.junit.jupiter.api.Assertions.*; |
| 12 | import static org.mockito.Mockito.*; |
| 13 | |
| 14 | @ExtendWith(MockitoExtension.class) |
| 15 | public class AdminServiceImplTest { |
| 16 | @Mock |
| 17 | private UserMapper userMapper; |
| 18 | @InjectMocks |
| 19 | private AdminServiceImpl adminService; |
| 20 | |
| 21 | @Test |
| 22 | public void testGrantVip_Success() { |
| 23 | User user = new User(); |
| 24 | user.setUserId(1L); |
| 25 | when(userMapper.selectById(1L)).thenReturn(user); |
| 26 | |
| 27 | boolean result = adminService.grantVip(1L); |
| 28 | assertTrue(result); |
| 29 | assertEquals("vip", user.getUserLevel()); |
| 30 | } |
| 31 | |
| 32 | @Test |
| 33 | public void testGrantVip_UserNotFound() { |
| 34 | when(userMapper.selectById(1L)).thenReturn(null); |
| 35 | boolean result = adminService.grantVip(1L); |
| 36 | assertFalse(result); |
| 37 | } |
| 38 | } |