yeg
2026-08-27 656cdaf4463b41a168adb83ccc05d8c2f03d4d78
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.ruoyi.system.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.ruoyi.system.domain.TTestType;
import com.ruoyi.system.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<TTestType> 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;
    }
}