啊鑫
5 天以前 1f963e2344833ff02087c05411b112147492bd00
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
package com.gs.xky.controller;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gs.xky.dto.NumbericalDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
 
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
/**
 * DingtalkController测试类
 *
 * @author 28567
 * @description 测试钉钉消息发送接口
 * @createDate 2025-01-27
 */
@WebMvcTest(DingtalkController.class)
public class DingtalkControllerTest {
 
    @Autowired
    private MockMvc mockMvc;
 
    @MockBean
    private com.gs.xky.service.DingtalkInfoService dingtalkInfoService;
 
    @Autowired
    private ObjectMapper objectMapper;
 
    @Test
    public void testSendMessage_Success() throws Exception {
        // 准备测试数据
        NumbericalDto requestDto = new NumbericalDto();
        requestDto.setReleaseNo("IQC202501270001");
 
        // Mock服务层返回成功
        when(dingtalkInfoService.sendMessage(anyString())).thenReturn(true);
 
        // 执行测试
        mockMvc.perform(post("/api/dingtalk/sendMessage")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(requestDto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.successful").value(0))
                .andExpect(jsonPath("$.data").value("接收成功"));
    }
 
    @Test
    public void testSendMessage_Failure() throws Exception {
        // 准备测试数据
        NumbericalDto requestDto = new NumbericalDto();
        requestDto.setReleaseNo("IQC202501270001");
 
        // Mock服务层返回失败
        when(dingtalkInfoService.sendMessage(anyString())).thenReturn(false);
 
        // 执行测试
        mockMvc.perform(post("/api/dingtalk/sendMessage")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(requestDto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(500))
                .andExpect(jsonPath("$.successful").value(1))
                .andExpect(jsonPath("$.data").value("接收失败"));
    }
 
    @Test
    public void testSendMessage_EmptyReleaseNo() throws Exception {
        // 准备测试数据 - 空的检验单号
        NumbericalDto requestDto = new NumbericalDto();
        requestDto.setReleaseNo("");
 
        // 执行测试
        mockMvc.perform(post("/api/dingtalk/sendMessage")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(requestDto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(500))
                .andExpect(jsonPath("$.message").value("检验单号不能为空"))
                .andExpect(jsonPath("$.successful").value(1))
                .andExpect(jsonPath("$.data").value("接收失败"));
    }
 
    @Test
    public void testSendMessage_NullReleaseNo() throws Exception {
        // 准备测试数据 - null检验单号
        NumbericalDto requestDto = new NumbericalDto();
        requestDto.setReleaseNo(null);
 
        // 执行测试
        mockMvc.perform(post("/api/dingtalk/sendMessage")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(requestDto)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(500))
                .andExpect(jsonPath("$.message").value("检验单号不能为空"))
                .andExpect(jsonPath("$.successful").value(1))
                .andExpect(jsonPath("$.data").value("接收失败"));
    }