testable-mock icon indicating copy to clipboard operation
testable-mock copied to clipboard

对junit4是不支持吗

Open hugo666-code opened this issue 5 years ago • 9 comments

hugo666-code avatar Feb 05 '21 06:02 hugo666-code

支持的,原理上不依赖任何开发框架、测试框架,通用

linfan avatar Feb 05 '21 06:02 linfan

image

@Test public void should_able_to_access_private_method() { List list = new ArrayList() {{ add("a"); add("b"); add("c"); }}; //assertEquals("member", demoPrivateAccess.privateFunc()); assertEquals("member", PrivateAccessor.invoke(demoPrivateAccess, "privateFunc")); // assertEquals("abc + hello + 1", demoPrivateAccess.privateFuncWithArgs(list, "hello", 1)); assertEquals("abc + hello + 1", PrivateAccessor.invoke(demoPrivateAccess, "privateFuncWithArgs", list, "hello", 1)); }

Error:java: com.alibaba.testable.processor.exception.MemberNotExistException: Method "privateFunc" with 0 parameter not exist in class "DemoPrivateAccess"

跑demo用junit4 编译直接就报错了. junit5正常

hugo666-code avatar Feb 05 '21 07:02 hugo666-code

1.版本用的0.4.11 改为0.4.5 没问题
2.@EnablePrivateAccess 私有方法在 0.4.11版本去掉@EnablePrivateAccess注解后也会正常,加上后就编译失败Error:java: com.alibaba.testable.processor.exception.MemberNotExistException: Method "privateFunc" with 0 parameter not exist in class "DemoPrivateAccess"

hugo666-code avatar Feb 05 '21 07:02 hugo666-code

是在Windows系统是使用吗? 看起来是0.4.9增加的私有方法访问目标检测功能在IDEA里运行的问题,JUnit 4我们内部项目有在用,包括Testable的回归测试项目也是用的JUnit 4。我们周末试下Windows环境的验证。

linfan avatar Feb 05 '21 10:02 linfan

Windows

hugo666-code avatar Feb 05 '21 10:02 hugo666-code

我先标为BUG,可能是系统兼容性问题,先做下验证

linfan avatar Feb 05 '21 11:02 linfan

在Windows下分别对Maven和Gradle方式导入项目到IntelliJ,然后在IntelliJ中以及控制台中运行测试的场景都进行了测试(共4种情况:Maven导入IntelliJ中运行,Gradle导入IntelliJ中运行,Maven在命令行运行,Gradle在cmd命令行运行),Testable版本0.4.11,目前没有复现出问题。

验证使用的代码在仓库的junit4分支: https://github.com/alibaba/testable-mock/tree/junit4/demo/java-demo

linfan avatar Feb 06 '21 13:02 linfan

作为临时方案,在下个版本里我们增加一个可选开关配置来关闭访问目标的编译期检查。

如果有其他能复现问题的线索或实例项目,也欢迎提供。

linfan avatar Feb 06 '21 13:02 linfan

0.4.12版本已增加控制开关 https://alibaba.github.io/testable-mock/#/zh-cn/doc/private-accessor?id=私有成员编译期校验

linfan avatar Feb 21 '21 07:02 linfan