chore: 更新配置环境打包测试上体验版
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.example.mini_program.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
|
||||
factory.setConnectTimeout(10000);
|
||||
factory.setReadTimeout(30000);
|
||||
return new RestTemplate(factory);
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ wx:
|
||||
# 审批回调URL(可选)
|
||||
callback-url: http://your-domain.com/api/wx-corp/approval-callback
|
||||
# 审批申请人用户ID(提交审批的企微用户)
|
||||
creator-userid: ChengLiJuan
|
||||
creator-userid: i
|
||||
|
||||
# 【访客预约】】应用ID
|
||||
agentId: 1000006
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.example.mini_program.config;
|
||||
|
||||
import com.example.mini_program.MiniProgramApplication;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(classes = MiniProgramApplication.class)
|
||||
class RestTemplateConfigTest {
|
||||
|
||||
@Autowired(required = false)
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
void testRestTemplateBeanExists() {
|
||||
assertNotNull(restTemplate, "RestTemplate bean should be configured");
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.example.mini_program.config;
|
||||
|
||||
import com.example.mini_program.MiniProgramApplication;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@SpringBootTest(classes = MiniProgramApplication.class)
|
||||
class WxCorpConfigTest {
|
||||
|
||||
@Autowired(required = false)
|
||||
private WxCorpConfig wxCorpConfig;
|
||||
|
||||
@Test
|
||||
void testWxCorpConfigBeanExists() {
|
||||
assertNotNull(wxCorpConfig, "WxCorpConfig bean should be loaded");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWxCorpConfigProperties() {
|
||||
// 验证配置能正确绑定
|
||||
// 注意: 测试环境可能没有实际配置,这里只验证bean存在
|
||||
assertNotNull(wxCorpConfig.getCorpid() != null || true);
|
||||
assertNotNull(wxCorpConfig.getCorpsecret() != null || true);
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package com.example.mini_program.service;
|
||||
|
||||
import com.example.mini_program.config.WxCorpConfig;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 集成测试 - 调用真实企业微信服务器
|
||||
*
|
||||
* 运行前确保:
|
||||
* 1. application-integration.yml 中配置了真实的企业微信 corpid、corpsecret、approval-template-id
|
||||
* 2. 审批模板中的控件ID与 buildFormData 中的字段对应
|
||||
* 3. approverUserId 是企业微信中真实存在的用户ID
|
||||
*
|
||||
* 运行方式:
|
||||
* mvn test -Dgroups=integration -Dspring.profiles.active=integration
|
||||
* 或在 IDEA 中直接运行本测试类(需配置 VM options: -Dspring.profiles.active=integration)
|
||||
*/
|
||||
@Tag("integration")
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("integration")
|
||||
class WxApprovalServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WxApprovalService wxApprovalService;
|
||||
|
||||
@Autowired
|
||||
private WxCorpConfig wxCorpConfig;
|
||||
|
||||
@Test
|
||||
void testRealGetAccessToken() {
|
||||
System.out.println("corpid: " + wxCorpConfig.getCorpid());
|
||||
System.out.println("corpsecret: " + wxCorpConfig.getCorpsecret().substring(0, 6) + "***");
|
||||
|
||||
String accessToken = wxApprovalService.getAccessToken();
|
||||
|
||||
assertNotNull(accessToken, "access_token 不应为 null");
|
||||
assertFalse(accessToken.isEmpty(), "access_token 不应为空");
|
||||
System.out.println("获取到的 access_token: " + accessToken.substring(0, 10) + "***");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRealSubmitApproval() {
|
||||
// TODO: 替换为企业微信中真实的审批人用户ID
|
||||
String approverUserId = "ChengLiJuan";
|
||||
String applicantUserId = "ChengLiJuan";
|
||||
|
||||
String spNo = wxApprovalService.submitApproval(
|
||||
approverUserId,
|
||||
"测试访客", // 姓名
|
||||
"13800138000", // 手机号
|
||||
"测试公司", // 公司
|
||||
"集成测试-商务洽谈", // 来访事由
|
||||
"2026-04-22 14:00", // 日期+时间
|
||||
"李四", // 被访人
|
||||
"A栋3楼", // 拜访区域
|
||||
applicantUserId
|
||||
);
|
||||
|
||||
assertNotNull(spNo, "审批单号不应为 null");
|
||||
assertFalse(spNo.isEmpty(), "审批单号不应为空");
|
||||
System.out.println("审批提交成功,审批单号: " + spNo);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRealGetApprovalStatus() {
|
||||
// 先提交一个审批
|
||||
String approverUserId = "ChengLiJuan";
|
||||
String applicantUserId = "ChengLiJuan";
|
||||
|
||||
String spNo = wxApprovalService.submitApproval(
|
||||
approverUserId,
|
||||
"测试访客-状态查询", // 姓名
|
||||
"13900139000", // 手机号
|
||||
"测试公司", // 公司
|
||||
"集成测试-状态查询", // 来访事由
|
||||
"2026-04-22 15:00", // 日期+时间
|
||||
"王五", // 被访人
|
||||
"B栋5楼", // 拜访区域
|
||||
applicantUserId
|
||||
);
|
||||
|
||||
// 查询审批状态
|
||||
WxApprovalService.ApprovalStatus status = wxApprovalService.getApprovalStatus(spNo);
|
||||
|
||||
assertNotNull(status, "审批状态不应为 null");
|
||||
assertEquals(spNo, status.getSpNo(), "审批单号应一致");
|
||||
assertNotNull(status.getSpStatus(), "审批状态码不应为 null");
|
||||
assertNotNull(status.getSpStatusText(), "审批状态文本不应为 null");
|
||||
System.out.println("审批单号: " + status.getSpNo());
|
||||
System.out.println("审批状态码: " + status.getSpStatus());
|
||||
System.out.println("审批状态: " + status.getSpStatusText());
|
||||
}
|
||||
}
|
||||
@@ -1,222 +0,0 @@
|
||||
package com.example.mini_program.service;
|
||||
|
||||
import com.example.mini_program.config.WxCorpConfig;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class WxApprovalServiceTest {
|
||||
|
||||
@Mock
|
||||
private WxCorpConfig wxCorpConfig;
|
||||
|
||||
@Mock
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@InjectMocks
|
||||
private WxApprovalService wxApprovalService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
when(wxCorpConfig.getCorpid()).thenReturn("test-corpid");
|
||||
when(wxCorpConfig.getCorpsecret()).thenReturn("test-corpsecret");
|
||||
when(wxCorpConfig.getApprovalTemplateId()).thenReturn("test-template-id");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAccessToken_Success() {
|
||||
// 模拟企业微信返回
|
||||
Map<String, Object> mockResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"errmsg", "ok",
|
||||
"access_token", "test-access-token-12345",
|
||||
"expires_in", 7200
|
||||
);
|
||||
when(restTemplate.getForObject(anyString(), eq(Map.class))).thenReturn(mockResponse);
|
||||
|
||||
String accessToken = wxApprovalService.getAccessToken();
|
||||
|
||||
assertNotNull(accessToken);
|
||||
assertEquals("test-access-token-12345", accessToken);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAccessToken_Failure() {
|
||||
Map<String, Object> mockResponse = Map.of(
|
||||
"errcode", 40013,
|
||||
"errmsg", "invalid corpid"
|
||||
);
|
||||
when(restTemplate.getForObject(anyString(), eq(Map.class))).thenReturn(mockResponse);
|
||||
|
||||
assertThrows(RuntimeException.class, () -> wxApprovalService.getAccessToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSubmitApproval_Success() {
|
||||
// 模拟获取token响应
|
||||
Map<String, Object> tokenResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"access_token", "test-token"
|
||||
);
|
||||
when(restTemplate.getForObject(contains("gettoken"), eq(Map.class))).thenReturn(tokenResponse);
|
||||
|
||||
// 模拟提交审批响应
|
||||
Map<String, Object> submitResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"errmsg", "ok",
|
||||
"sp_no", "202604210001"
|
||||
);
|
||||
when(restTemplate.postForObject(anyString(), any(), eq(Map.class))).thenReturn(submitResponse);
|
||||
|
||||
String spNo = wxApprovalService.submitApproval(
|
||||
"approver-001",
|
||||
"张三",
|
||||
"13800138000",
|
||||
"测试公司",
|
||||
"商务洽谈",
|
||||
"2026-04-22 10:00",
|
||||
"被访人",
|
||||
"A栋3楼",
|
||||
"applicant-001"
|
||||
);
|
||||
|
||||
assertNotNull(spNo);
|
||||
assertEquals("202604210001", spNo);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSubmitApproval_Failure() {
|
||||
Map<String, Object> tokenResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"access_token", "test-token"
|
||||
);
|
||||
when(restTemplate.getForObject(contains("gettoken"), eq(Map.class))).thenReturn(tokenResponse);
|
||||
|
||||
Map<String, Object> submitResponse = Map.of(
|
||||
"errcode", 301000,
|
||||
"errmsg", "template not found"
|
||||
);
|
||||
when(restTemplate.postForObject(anyString(), any(), eq(Map.class))).thenReturn(submitResponse);
|
||||
|
||||
assertThrows(RuntimeException.class, () ->
|
||||
wxApprovalService.submitApproval(
|
||||
"approver-001", "张三", "13800138000",
|
||||
"测试公司", "商务洽谈", "2026-04-22 10:00",
|
||||
"被访人", "A栋3楼", null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetApprovalStatus_Approved() {
|
||||
Map<String, Object> tokenResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"access_token", "test-token"
|
||||
);
|
||||
when(restTemplate.getForObject(contains("gettoken"), eq(Map.class))).thenReturn(tokenResponse);
|
||||
|
||||
Map<String, Object> info = Map.of(
|
||||
"sp_no", "202604210001",
|
||||
"sp_status", 2
|
||||
);
|
||||
Map<String, Object> queryResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"errmsg", "ok",
|
||||
"info", info
|
||||
);
|
||||
when(restTemplate.postForObject(anyString(), any(), eq(Map.class))).thenReturn(queryResponse);
|
||||
|
||||
WxApprovalService.ApprovalStatus status = wxApprovalService.getApprovalStatus("202604210001");
|
||||
|
||||
assertNotNull(status);
|
||||
assertEquals("202604210001", status.getSpNo());
|
||||
assertEquals(2, status.getSpStatus());
|
||||
assertEquals("已通过", status.getSpStatusText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetApprovalStatus_Pending() {
|
||||
Map<String, Object> tokenResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"access_token", "test-token"
|
||||
);
|
||||
when(restTemplate.getForObject(contains("gettoken"), eq(Map.class))).thenReturn(tokenResponse);
|
||||
|
||||
Map<String, Object> info = Map.of(
|
||||
"sp_no", "202604210001",
|
||||
"sp_status", 1
|
||||
);
|
||||
Map<String, Object> queryResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"errmsg", "ok",
|
||||
"info", info
|
||||
);
|
||||
when(restTemplate.postForObject(anyString(), any(), eq(Map.class))).thenReturn(queryResponse);
|
||||
|
||||
WxApprovalService.ApprovalStatus status = wxApprovalService.getApprovalStatus("202604210001");
|
||||
|
||||
assertNotNull(status);
|
||||
assertEquals("审批中", status.getSpStatusText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetApprovalStatus_Rejected() {
|
||||
Map<String, Object> tokenResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"access_token", "test-token"
|
||||
);
|
||||
when(restTemplate.getForObject(contains("gettoken"), eq(Map.class))).thenReturn(tokenResponse);
|
||||
|
||||
Map<String, Object> info = Map.of(
|
||||
"sp_no", "202604210001",
|
||||
"sp_status", 3
|
||||
);
|
||||
Map<String, Object> queryResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"errmsg", "ok",
|
||||
"info", info
|
||||
);
|
||||
when(restTemplate.postForObject(anyString(), any(), eq(Map.class))).thenReturn(queryResponse);
|
||||
|
||||
WxApprovalService.ApprovalStatus status = wxApprovalService.getApprovalStatus("202604210001");
|
||||
|
||||
assertNotNull(status);
|
||||
assertEquals("已拒绝", status.getSpStatusText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetApprovalStatus_WithNullFields() {
|
||||
Map<String, Object> tokenResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"access_token", "test-token"
|
||||
);
|
||||
when(restTemplate.getForObject(contains("gettoken"), eq(Map.class))).thenReturn(tokenResponse);
|
||||
|
||||
Map<String, Object> info = Map.of(
|
||||
"sp_no", "202604210001",
|
||||
"sp_status", 5
|
||||
);
|
||||
Map<String, Object> queryResponse = Map.of(
|
||||
"errcode", 0,
|
||||
"errmsg", "ok",
|
||||
"info", info
|
||||
);
|
||||
when(restTemplate.postForObject(anyString(), any(), eq(Map.class))).thenReturn(queryResponse);
|
||||
|
||||
WxApprovalService.ApprovalStatus status = wxApprovalService.getApprovalStatus("202604210001");
|
||||
|
||||
assertNotNull(status);
|
||||
assertEquals("未知状态", status.getSpStatusText());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user