package com.ruoyi.system.service.impl; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.Arrays; import java.util.Collections; import java.util.List; 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.junit.jupiter.MockitoExtension; import com.ruoyi.system.domain.TBatteryGroup; import com.ruoyi.system.domain.TBatteryGroupBattery; import com.ruoyi.system.domain.TBatteryGroupRelationField; import com.ruoyi.system.domain.TBatteryGroupTemplate; import com.ruoyi.system.mapper.TBatteryGroupMapper; /** 电池组 Service 单元测试。 */ @ExtendWith(MockitoExtension.class) class TBatteryGroupServiceImplTest { @Mock private TBatteryGroupMapper batteryGroupMapper; @Test void listShouldIncludeSharedFieldValues() { TBatteryGroup group = new TBatteryGroup(); group.setGroupId(12L); TBatteryGroupRelationField relation = new TBatteryGroupRelationField(); relation.setBatteryGroupFieldId(7L); relation.setFieldName("额定容量"); relation.setFieldValue("72"); when(batteryGroupMapper.selectBatteryGroupList(any(TBatteryGroup.class))).thenReturn(Collections.singletonList(group)); when(batteryGroupMapper.selectRelationFields(12L)).thenReturn(Collections.singletonList(relation)); List result = batteryGroupService.selectBatteryGroupList(new TBatteryGroup()); assertEquals("额定容量", result.get(0).getTemplateFields().get(0).getFieldName()); assertEquals("72", result.get(0).getTemplateFields().get(0).getFieldValue()); } @InjectMocks private TBatteryGroupServiceImpl batteryGroupService; @Test void insertShouldPersistBatteriesWithoutTemplate() { TBatteryGroup group = new TBatteryGroup(); group.setGroupName(" 样品组 "); group.setModel("M-01"); group.setBatteryNos(Arrays.asList(" BAT-01 ", "BAT-02")); group.setCreateBy("admin"); when(batteryGroupMapper.selectBatteryGroupByName("样品组")).thenReturn(null); when(batteryGroupMapper.selectBatteryByNo(any(String.class), any())).thenReturn(null); doAnswer(invocation -> { invocation.getArgument(0).setGroupId(12L); return 1; }).when(batteryGroupMapper).insertBatteryGroup(any(TBatteryGroup.class)); assertEquals(1, batteryGroupService.insertBatteryGroup(group)); ArgumentCaptor> captor = ArgumentCaptor.forClass(List.class); verify(batteryGroupMapper).batchInsertBatteries(captor.capture()); assertEquals(Arrays.asList("BAT-01", "BAT-02"), captor.getValue().stream() .map(TBatteryGroupBattery::getBatteryNo).toList()); verify(batteryGroupMapper, never()).batchInsertRelationFields(anyList()); } @Test void updateShouldPersistStatusAndTemplateFieldValue() { TBatteryGroup current = new TBatteryGroup(); current.setGroupId(12L); current.setStatus("0"); TBatteryGroup group = new TBatteryGroup(); group.setGroupId(12L); group.setGroupName("样品组"); group.setModel("M-01"); group.setStatus("1"); group.setBatteryNos(Collections.singletonList("BAT-01")); group.setTemplateId(5L); TBatteryGroupRelationField relation = new TBatteryGroupRelationField(); relation.setBatteryGroupFieldId(7L); relation.setFieldValue("72"); relation.setFieldType("number"); group.setTemplateFieldRelations(Collections.singletonList(relation)); when(batteryGroupMapper.selectBatteryGroupById(12L)).thenReturn(current); when(batteryGroupMapper.countTestingByGroupId(12L)).thenReturn(0); when(batteryGroupMapper.selectBatteryByNo("BAT-01", 12L)).thenReturn(null); when(batteryGroupMapper.selectTemplate(5L)).thenReturn(new TBatteryGroupTemplate()); when(batteryGroupMapper.selectTemplateFieldIds(5L)).thenReturn(Collections.singletonList(7L)); when(batteryGroupMapper.selectValidTemplateFields(5L, Collections.singletonList(7L))) .thenReturn(Collections.singletonList(relation)); when(batteryGroupMapper.updateBatteryGroup(group)).thenReturn(1); assertEquals(1, batteryGroupService.updateBatteryGroup(group)); verify(batteryGroupMapper).updateBatteryGroup(group); ArgumentCaptor> captor = ArgumentCaptor.forClass(List.class); verify(batteryGroupMapper).batchInsertRelationFields(captor.capture()); assertEquals("72", captor.getValue().get(0).getFieldValue()); assertEquals("1", group.getStatus()); } @Test void insertShouldRejectInvalidTypedFieldValue() { TBatteryGroup group = new TBatteryGroup(); group.setGroupName("样品组"); group.setModel("M-01"); group.setBatteryNos(Collections.singletonList("BAT-01")); group.setTemplateId(5L); TBatteryGroupRelationField relation = new TBatteryGroupRelationField(); relation.setBatteryGroupFieldId(7L); relation.setFieldValue("72Ah"); group.setTemplateFieldRelations(Collections.singletonList(relation)); TBatteryGroupRelationField validField = new TBatteryGroupRelationField(); validField.setBatteryGroupFieldId(7L); validField.setFieldType("number"); when(batteryGroupMapper.selectBatteryGroupByName("样品组")).thenReturn(null); when(batteryGroupMapper.selectBatteryByNo("BAT-01", null)).thenReturn(null); when(batteryGroupMapper.selectTemplate(5L)).thenReturn(new TBatteryGroupTemplate()); when(batteryGroupMapper.selectTemplateFieldIds(5L)).thenReturn(Collections.singletonList(7L)); when(batteryGroupMapper.selectValidTemplateFields(5L, Collections.singletonList(7L))) .thenReturn(Collections.singletonList(validField)); org.junit.jupiter.api.Assertions.assertThrows(com.ruoyi.common.exception.ServiceException.class, () -> batteryGroupService.insertBatteryGroup(group)); verify(batteryGroupMapper, never()).insertBatteryGroup(any(TBatteryGroup.class)); } }