yeg
2026-08-29 795c16bd3aa31f4faa4fe8a8e2155718cac0e5d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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<TBatteryGroup> 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.<TBatteryGroup>getArgument(0).setGroupId(12L);
            return 1;
        }).when(batteryGroupMapper).insertBatteryGroup(any(TBatteryGroup.class));
 
        assertEquals(1, batteryGroupService.insertBatteryGroup(group));
 
        ArgumentCaptor<List<TBatteryGroupBattery>> 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<List<TBatteryGroupRelationField>> 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));
    }
}