Skip to content

Commit bd0ba53

Browse files
Copilotbinarywang
andcommitted
清理测试代码:移除调试输出,使用反射调用生产代码,添加 @OverRide 注解
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
1 parent a256272 commit bd0ba53

File tree

1 file changed

+18
-33
lines changed

1 file changed

+18
-33
lines changed

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/util/RsaCryptoUtilTest.java

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,14 @@ public void testFindAnnotatedFieldsInNestedClass() {
3232
Class<?> receiverClass = receiver.getClass();
3333
Field[] fields = receiverClass.getDeclaredFields();
3434

35-
System.out.println("=== Receiver 类中的所有字段 ===");
3635
boolean foundNameField = false;
3736
boolean nameFieldHasAnnotation = false;
3837

3938
for (Field field : fields) {
40-
System.out.println("字段名: " + field.getName() + ", 类型: " + field.getType().getName());
4139
if (field.getName().equals("name")) {
4240
foundNameField = true;
4341
if (field.isAnnotationPresent(SpecEncrypt.class)) {
4442
nameFieldHasAnnotation = true;
45-
System.out.println(" -> name 字段有 @SpecEncrypt 注解");
46-
} else {
47-
System.out.println(" -> name 字段没有 @SpecEncrypt 注解");
4843
}
4944
}
5045
}
@@ -85,13 +80,11 @@ public void testEncryptFieldsWithNestedObjects() {
8580
try {
8681
Field receiversField = ProfitSharingV3Request.class.getDeclaredField("receivers");
8782
boolean hasAnnotation = receiversField.isAnnotationPresent(SpecEncrypt.class);
88-
System.out.println("ProfitSharingV3Request.receivers 字段有 @SpecEncrypt 注解: " + hasAnnotation);
8983
assertTrue(hasAnnotation, "receivers 字段应该有 @SpecEncrypt 注解");
9084
} catch (NoSuchFieldException e) {
9185
fail("应该能找到 receivers 字段");
9286
}
9387

94-
System.out.println("测试对象创建成功,name字段: " + receiver.getName());
9588
// 验证name字段不为null
9689
assertNotNull(receiver.getName());
9790
assertEquals(receiver.getName(), "张三");
@@ -147,6 +140,11 @@ class ChildRequest extends ParentRequest {
147140
@SpecEncrypt
148141
@SerializedName("child_name")
149142
private String childName;
143+
144+
@Override
145+
protected boolean canEqual(final Object other) {
146+
return other instanceof ChildRequest;
147+
}
150148
}
151149

152150
// 创建子类实例
@@ -155,40 +153,27 @@ class ChildRequest extends ParentRequest {
155153
request.setChildName("子类字段");
156154

157155
// 验证能够找到父类和子类的字段
158-
System.out.println("=== 测试继承场景 ===");
159-
System.out.println("父类字段值: " + request.getParentName());
160-
System.out.println("子类字段值: " + request.getChildName());
161-
162156
// 使用 getDeclaredFields 只能找到子类字段
163157
Field[] childFields = ChildRequest.class.getDeclaredFields();
164-
System.out.println("使用 getDeclaredFields 找到的字段数: " + childFields.length);
165-
166-
// 使用 getAllFields 辅助方法应该能找到父类和子类的所有字段
167-
List<Field> allFields = getAllFields(ChildRequest.class);
168-
System.out.println("使用 getAllFields 找到的字段数: " + allFields.size());
169158

159+
// 使用反射调用 RsaCryptoUtil 的私有 getAllFields 方法
170160
int annotatedFieldCount = 0;
171-
for (Field field : allFields) {
172-
if (field.isAnnotationPresent(SpecEncrypt.class)) {
173-
annotatedFieldCount++;
174-
System.out.println(" -> 找到带 @SpecEncrypt 注解的字段: " + field.getName());
161+
try {
162+
java.lang.reflect.Method getAllFieldsMethod = RsaCryptoUtil.class.getDeclaredMethod("getAllFields", Class.class);
163+
getAllFieldsMethod.setAccessible(true);
164+
@SuppressWarnings("unchecked")
165+
List<Field> allFields = (List<Field>) getAllFieldsMethod.invoke(null, ChildRequest.class);
166+
167+
for (Field field : allFields) {
168+
if (field.isAnnotationPresent(SpecEncrypt.class)) {
169+
annotatedFieldCount++;
170+
}
175171
}
172+
} catch (Exception e) {
173+
fail("无法调用 getAllFields 方法: " + e.getMessage());
176174
}
177175

178176
// 应该找到2个带注解的字段(parentName 和 childName)
179177
assertTrue(annotatedFieldCount >= 2, "应该能找到至少2个带 @SpecEncrypt 注解的字段");
180178
}
181-
182-
/**
183-
* 辅助方法:递归获取类的所有字段,包括父类中的字段
184-
*/
185-
private List<Field> getAllFields(Class<?> clazz) {
186-
List<Field> fields = new ArrayList<>();
187-
while (clazz != null && clazz != Object.class) {
188-
Field[] declaredFields = clazz.getDeclaredFields();
189-
java.util.Collections.addAll(fields, declaredFields);
190-
clazz = clazz.getSuperclass();
191-
}
192-
return fields;
193-
}
194179
}

0 commit comments

Comments
 (0)