test: 为覆盖率最低的 weixin-java-open、weixin-java-channel 补充单元测试 - #4080
test: 为覆盖率最低的 weixin-java-open、weixin-java-channel 补充单元测试#4080devin-ai-integration[bot] wants to merge 3 commits into
Conversation
覆盖率最低的两个模块补充纯逻辑单元测试: - open: WxOpenCryptUtil、WxOpenXmlMessage、WxOpenGsonBuilder 适配器 - channel: XmlUtils、WxChCryptUtils Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 40f45b483d
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| /** | ||
| * {@link WxOpenCryptUtil} 单元测试 | ||
| */ | ||
| public class WxOpenCryptUtilTest { |
There was a problem hiding this comment.
在 weixin-java-open/pom.xml:122-124 中该模块固定通过 src/test/resources/testng.xml 运行 TestNG suite,Surefire 官方文档说明 suite 配置会覆盖默认扫描并运行 suite 文件中的测试(https://maven.apache.org/surefire-archives/surefire-3.5.4/maven-surefire-plugin/examples/testng.html#using-suite-xml-files);但当前 suite 只列出 3 个 bean.result 测试,没有包含这个类、WxOpenXmlMessageTest 或 WxOpenGsonBuilderTest,所以启用常规 mvn -pl weixin-java-open -am test 时本 PR 新增的 Open 覆盖率不会执行。请把这些类加入 suite,或调整 Surefire 配置让默认扫描生效。
Useful? React with 👍 / 👎.
| /** | ||
| * {@link WxChCryptUtils} 单元测试 | ||
| */ | ||
| public class WxChCryptUtilsTest { |
There was a problem hiding this comment.
weixin-java-channel/pom.xml:131-133 同样固定使用 src/test/resources/testng.xml,而该 suite 的 <classes> 在 weixin-java-channel/src/test/resources/testng.xml:5-9 中没有任何启用的测试类;按 Surefire suite XML 语义(https://maven.apache.org/surefire-archives/surefire-3.5.4/maven-surefire-plugin/examples/testng.html#using-suite-xml-files),启用常规 mvn -pl weixin-java-channel -am test 时既不会执行这个类,也不会执行同 PR 新增的 XmlUtilsTest,导致新增 Channel 单测没有回归保护。请将新增类加入 suite,或移除 suiteXmlFiles 让默认扫描生效。
Useful? React with 👍 / 👎.
binarywang
left a comment
There was a problem hiding this comment.
需要补充测试接入配置后再合并:
本 PR 新增的 5 个 TestNG 测试类没有加入 weixin-java-open 和 weixin-java-channel 各自的 src/test/resources/testng.xml。两个模块的 Surefire 都显式指定该 suite;open 的 suite 只列出既有测试,channel 的 classes 为空,因此常规 mvn test / CI 不会执行这些新测试,无法形成持续回归保护。
请将新增测试类加入对应 suite(或调整 Surefire 配置为自动发现),并以模块测试命令验证它们确实被执行。
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
已修复(commit 75f347c):在两个模块的 验证(临时将根
open 模块唯一失败的是既有测试 |
…序列化异常 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
已按确认修复
|
Summary
统计各模块
src/main/java与src/test/java文件数后,测试占比最低的是weixin-java-open(240 主类 / 13 测试)和weixin-java-channel(674 / 36),且这两个模块已有测试几乎全是需要真实微信凭据的*ServiceImplTest,本地不可执行。本 PR 新增不依赖网络与凭据的纯逻辑单元测试,并顺带修复由此暴露的一个既有反序列化 bug。新增测试(15 个用例)
WxOpenCryptUtilTest:encrypt→ 解析<Encrypt>/<MsgSignature>/<TimeStamp>/<Nonce>→decryptXml/decryptContent往返一致;签名不匹配抛WxRuntimeException;componentAesKey含空格时与去空格版本加密结果一致(覆盖构造函数中的StringUtils.remove(encodingAesKey, " "))。WxOpenXmlMessageTest:fromXml解析component_verify_ticket、authorized、快速创建小程序推送(appid/status/auth_code/info.name,含getRegistAppId()兼容读取);fromXml(InputStream);fromEncryptedXml(String/InputStream)往返并校验context为解密后的原始 xml。WxOpenGsonBuilderTest:create()单例;WxOpenComponentAccessToken、WxOpenAuthorizerAccessToken、WxOpenAuthorizationInfo(含func_info缺失funcscope_category元素被跳过)、WxOpenQueryAuthResult各适配器的反序列化;disableHtmlEscaping行为。XmlUtilsTest(channel):encode、encode(ObjectMapper, obj)、decode(String/TypeReference/InputStream),以及未知字段被忽略、null/空串/非法 xml 返回null的容错分支。WxChCryptUtilsTest(channel):回调消息加解密往返与签名校验失败分支;静态decrypt(sessionKey, encryptedData, iv)用本地 AES/CBC 加密数据做往返验证,以及非法密钥抛异常。测试中的 token / EncodingAESKey 均为明显占位值(AES key 为
base64("0123456789abcdef" * 2)去掉补位),不含任何真实凭据。测试接入配置
两个模块的 Surefire 都显式指定
src/test/resources/testng.xml,因此在两个 suite 中新增Util_Test注册新增测试类;channel 侧顺带把此前未接入 suite 的JsonUtilsTest、ResponseUtilsTest一并加入,保证mvn test能真正执行。修复
WxFastMaCanSetCategoryResult的 errcode 字段冲突接入 suite 后暴露既有测试
WxFastMaCanSetCategoryResultTest#testFromJson失败:该 Bean 自带@SerializedName("errcode") private int errCode,与父类WxOpenResult#errcode映射到同一 JSON 字段名,新版本 Gson 直接抛IllegalArgumentException: declares multiple JSON fields named 'errcode',导致该结果类完全无法反序列化。移除重复字段,改为委托父类
errcode的过时方法,保留getErrCode()/setErrCode(int)的源码兼容性;既有测试补充断言getErrcode()=="0"、getErrCode()==0、isSuccess()作为回归保护。验证
根
pom.xml中 surefire 配置为<skip>true</skip>,默认不执行测试。验证时临时将其改为false运行,提交前已还原(diff 中不包含pom.xml):本地环境为 JDK 17 + Maven 3.6.3;新增代码仅使用 Java 8 语法与 TestNG,遵循各模块既有测试风格。
Link to Devin session: https://app.devin.ai/sessions/14ac57aaa8c4438da6161b7ddb001650
Requested by: @binarywang