@@ -52,6 +52,113 @@ public void setup() {
5252 payService .setMultiConfig (configMap );
5353 }
5454
55+ /**
56+ * 测试直接通过 mchId 和 appId 获取配置(新功能)
57+ */
58+ @ Test
59+ public void testGetConfigWithMchIdAndAppId () {
60+ // 测试获取第一个配置
61+ WxPayConfig config1 = payService .getConfig (testMchId , testAppId1 );
62+ assertNotNull (config1 , "应该能够获取到配置" );
63+ assertEquals (config1 .getMchId (), testMchId );
64+ assertEquals (config1 .getAppId (), testAppId1 );
65+ assertEquals (config1 .getMchKey (), "test_key_1" );
66+
67+ // 测试获取第二个配置
68+ WxPayConfig config2 = payService .getConfig (testMchId , testAppId2 );
69+ assertNotNull (config2 );
70+ assertEquals (config2 .getAppId (), testAppId2 );
71+ assertEquals (config2 .getMchKey (), "test_key_2" );
72+
73+ // 测试获取第三个配置
74+ WxPayConfig config3 = payService .getConfig (testMchId , testAppId3 );
75+ assertNotNull (config3 );
76+ assertEquals (config3 .getAppId (), testAppId3 );
77+ assertEquals (config3 .getMchKey (), "test_key_3" );
78+ }
79+
80+ /**
81+ * 测试直接通过 mchId 获取配置(新功能)
82+ */
83+ @ Test
84+ public void testGetConfigWithMchIdOnly () {
85+ WxPayConfig config = payService .getConfig (testMchId );
86+ assertNotNull (config , "应该能够通过mchId获取配置" );
87+ assertEquals (config .getMchId (), testMchId );
88+
89+ // appId应该是三个中的一个
90+ String currentAppId = config .getAppId ();
91+ assertTrue (
92+ testAppId1 .equals (currentAppId ) || testAppId2 .equals (currentAppId ) || testAppId3 .equals (currentAppId ),
93+ "获取的配置的appId应该是配置的appId之一"
94+ );
95+ }
96+
97+ /**
98+ * 测试 getConfig 方法不依赖 ThreadLocal
99+ * 在不切换配置的情况下也能直接获取
100+ */
101+ @ Test
102+ public void testGetConfigWithoutSwitchover () {
103+ // 不进行任何switchover操作,直接通过参数获取配置
104+ WxPayConfig config1 = payService .getConfig (testMchId , testAppId1 );
105+ WxPayConfig config2 = payService .getConfig (testMchId , testAppId2 );
106+ WxPayConfig config3 = payService .getConfig (testMchId , testAppId3 );
107+
108+ // 验证可以同时获取到所有配置,不受 ThreadLocal 影响
109+ assertNotNull (config1 );
110+ assertNotNull (config2 );
111+ assertNotNull (config3 );
112+
113+ assertEquals (config1 .getAppId (), testAppId1 );
114+ assertEquals (config2 .getAppId (), testAppId2 );
115+ assertEquals (config3 .getAppId (), testAppId3 );
116+ }
117+
118+ /**
119+ * 测试 getConfig 方法处理不存在的配置
120+ */
121+ @ Test
122+ public void testGetConfigWithNonexistentConfig () {
123+ // 测试不存在的商户号和appId组合
124+ WxPayConfig config = payService .getConfig ("nonexistent_mch_id" , "nonexistent_app_id" );
125+ assertNull (config , "获取不存在的配置应该返回null" );
126+
127+ // 测试存在商户号但不存在的appId
128+ config = payService .getConfig (testMchId , "wx9999999999999999" );
129+ assertNull (config , "获取不存在的appId配置应该返回null" );
130+ }
131+
132+ /**
133+ * 测试 getConfig 方法处理空参数或null参数
134+ */
135+ @ Test
136+ public void testGetConfigWithNullOrEmptyParameters () {
137+ // 测试 null 商户号
138+ WxPayConfig config = payService .getConfig (null , testAppId1 );
139+ assertNull (config , "商户号为null时应该返回null" );
140+
141+ // 测试空商户号
142+ config = payService .getConfig ("" , testAppId1 );
143+ assertNull (config , "商户号为空字符串时应该返回null" );
144+
145+ // 测试 null appId
146+ config = payService .getConfig (testMchId , null );
147+ assertNull (config , "appId为null时应该返回null" );
148+
149+ // 测试空 appId
150+ config = payService .getConfig (testMchId , "" );
151+ assertNull (config , "appId为空字符串时应该返回null" );
152+
153+ // 测试仅mchId方法的null参数
154+ config = payService .getConfig ((String ) null );
155+ assertNull (config , "商户号为null时应该返回null" );
156+
157+ // 测试仅mchId方法的空字符串
158+ config = payService .getConfig ("" );
159+ assertNull (config , "商户号为空字符串时应该返回null" );
160+ }
161+
55162 /**
56163 * 测试使用 mchId + appId 精确切换(原有功能,确保向后兼容)
57164 */
0 commit comments