zjk
2026-09-24 4c4b36e7337385ff15ff66a0ad487cdffaf9c86b
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
package com.tcm.training.admin.member.service;
 
import cn.idev.excel.FastExcel;
import com.tcm.training.admin.member.mapper.ClinicQueryMapper;
import com.tcm.training.admin.member.row.ClinicListRow;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletResponse;
 
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
 
class ClinicExportTest {
 
    @Test
    void shouldExportAddressAgentAndPlainPhone() throws Exception {
        ClinicListRow row = new ClinicListRow();
        row.setPhone("13800138000");
        row.setAddress("人民路88号");
        row.setAgentName("测试省代");
 
        List<Map<Integer, String>> sheet = exportSheet(row);
 
        assertEquals(Arrays.asList("序号", "诊所", "电话", "诊所区域", "详细地址",
                "省代", "绑定用户数", "状态", "注册时间"),
                Arrays.asList(sheet.get(0).values().toArray(new String[0])));
        assertEquals("13800138000", sheet.get(1).get(2));
        assertEquals("人民路88号", sheet.get(1).get(4));
        assertEquals("测试省代", sheet.get(1).get(5));
    }
 
    @Test
    void shouldUsePlaceholderForMissingAddressAndAgent() throws Exception {
        List<Map<Integer, String>> sheet = exportSheet(new ClinicListRow());
 
        assertEquals("-", sheet.get(1).get(4));
        assertEquals("-", sheet.get(1).get(5));
    }
 
    /** 读取实际生成的 Excel,同时验证表头顺序和字段映射。 */
    private List<Map<Integer, String>> exportSheet(ClinicListRow row) throws Exception {
        ClinicQueryMapper mapper = mock(ClinicQueryMapper.class);
        when(mapper.selectClinicList(null, null, null, null, null, null, null))
                .thenReturn(Collections.singletonList(row));
        ClinicAppService service = new ClinicAppService(mapper, null, null, null, null, null);
        MockHttpServletResponse response = new MockHttpServletResponse();
        service.export(null, null, null, null, null, null, null, response);
        return FastExcel.read(new ByteArrayInputStream(response.getContentAsByteArray()))
                .headRowNumber(0).sheet().doReadSync();
    }
}