package com.battery.business.service.impl; 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.anyLong; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import com.ruoyi.common.exception.ServiceException; import com.battery.business.domain.TTestType; import com.battery.business.mapper.TTestTypeMapper; /** * 测试类型 Service 单元测试。 */ @ExtendWith(MockitoExtension.class) class TTestTypeServiceImplTest { @Mock private TTestTypeMapper testTypeMapper; @Spy @InjectMocks private TTestTypeServiceImpl testTypeService; private TTestType testType; @BeforeEach void setUp() { testType = new TTestType(); testType.setTestTypeId(1L); testType.setTestTypeName("容量测试"); testType.setStatus("0"); } @Test void insertTestTypeShouldNormalizeFieldsAndInitializeStatus() { testType.setTestTypeId(null); testType.setTestTypeName(" 容量测试 "); testType.setRemark(" 备注 "); when(testTypeMapper.insertTestType(any(TTestType.class))).thenReturn(1); int rows = testTypeService.insertTestType(testType); ArgumentCaptor captor = ArgumentCaptor.forClass(TTestType.class); verify(testTypeMapper).insertTestType(captor.capture()); TTestType saved = captor.getValue(); assertEquals(1, rows); assertEquals("容量测试", saved.getTestTypeName()); assertEquals("备注", saved.getRemark()); assertEquals("0", saved.getStatus()); assertEquals("0", saved.getDelFlag()); } @Test void insertTestTypeShouldRejectBlankName() { testType.setTestTypeId(null); testType.setTestTypeName(" "); ServiceException exception = assertThrows(ServiceException.class, () -> testTypeService.insertTestType(testType)); assertEquals("测试类型名称不能为空", exception.getMessage()); verify(testTypeMapper, never()).insertTestType(any(TTestType.class)); } @Test void insertTestTypeShouldRejectDuplicateName() { testType.setTestTypeId(null); TTestType existing = testType(2L, "容量测试", "0"); when(testTypeMapper.selectTestTypeByName("容量测试")).thenReturn(existing); ServiceException exception = assertThrows(ServiceException.class, () -> testTypeService.insertTestType(testType)); assertEquals("测试类型名称已存在", exception.getMessage()); verify(testTypeMapper, never()).insertTestType(any(TTestType.class)); } @Test void updateTestTypeShouldAllowCurrentName() { when(testTypeMapper.selectTestTypeById(1L)).thenReturn(testType); when(testTypeMapper.selectTestTypeByName("容量测试")).thenReturn(testType); when(testTypeMapper.updateTestType(any(TTestType.class))).thenReturn(1); assertEquals(1, testTypeService.updateTestType(testType)); verify(testTypeMapper).updateTestType(testType); } @Test void updateTestTypeStatusShouldRejectInvalidStatus() { TTestType command = statusCommand("2"); ServiceException exception = assertThrows(ServiceException.class, () -> testTypeService.updateTestTypeStatus(command)); assertEquals("状态只能为0或1", exception.getMessage()); verify(testTypeMapper, never()).updateTestTypeStatus(any(TTestType.class)); } @Test void updateTestTypeStatusShouldFreezeTestType() { when(testTypeMapper.selectTestTypeById(1L)).thenReturn(testType); when(testTypeMapper.updateTestTypeStatus(any(TTestType.class))).thenReturn(1); TTestType command = statusCommand("1"); assertEquals(1, testTypeService.updateTestTypeStatus(command)); verify(testTypeMapper).updateTestTypeStatus(command); } @Test void updateTestTypeStatusShouldBeIdempotent() { when(testTypeMapper.selectTestTypeById(1L)).thenReturn(testType); TTestType command = statusCommand("0"); assertEquals(1, testTypeService.updateTestTypeStatus(command)); verify(testTypeMapper, never()).updateTestTypeStatus(any(TTestType.class)); } @Test void deleteTestTypeByIdsShouldRejectReferencedTypeBeforeAnyDelete() { TTestType first = testType(1L, "容量测试", "0"); TTestType referenced = testType(2L, "循环测试", "0"); when(testTypeMapper.selectTestTypeById(1L)).thenReturn(first); when(testTypeMapper.selectTestTypeById(2L)).thenReturn(referenced); doReturn(0).when(testTypeService).countTestTypeReferences(1L); doReturn(1).when(testTypeService).countTestTypeReferences(2L); ServiceException exception = assertThrows(ServiceException.class, () -> testTypeService.deleteTestTypeByIds(new Long[] { 1L, 2L }, "admin")); assertEquals("测试类型“循环测试”已被使用,不能删除", exception.getMessage()); verify(testTypeMapper, never()).deleteTestTypeById(anyLong(), any(String.class)); } @Test void deleteTestTypeByIdsShouldDeleteAllValidatedTypes() { when(testTypeMapper.selectTestTypeById(1L)).thenReturn(testType(1L, "容量测试", "0")); when(testTypeMapper.selectTestTypeById(2L)).thenReturn(testType(2L, "循环测试", "0")); when(testTypeMapper.deleteTestTypeById(anyLong(), any(String.class))).thenReturn(1); assertEquals(2, testTypeService.deleteTestTypeByIds(new Long[] { 1L, 2L }, "admin")); verify(testTypeMapper).deleteTestTypeById(1L, "admin"); verify(testTypeMapper).deleteTestTypeById(2L, "admin"); } @Test void countTestTypeReferencesShouldQueryActiveTests() { when(testTypeMapper.countTestReferencesByTestTypeId(1L)).thenReturn(2); assertEquals(2, testTypeService.countTestTypeReferences(1L)); verify(testTypeMapper).countTestReferencesByTestTypeId(1L); } private TTestType statusCommand(String status) { TTestType command = new TTestType(); command.setTestTypeId(1L); command.setStatus(status); command.setUpdateBy("admin"); return command; } private TTestType testType(Long id, String name, String status) { TTestType value = new TTestType(); value.setTestTypeId(id); value.setTestTypeName(name); value.setStatus(status); return value; } }