你能对混淆代码进行单元测试吗?
我希望在现有的 Ant 构建脚本中混淆 Java Web 应用程序代码,但在单元测试方面遇到了问题。 我在代码编译后、jar 化之前以及运行单元测试之前对代码进行了混淆。
但是,如果我混淆生产代码而不是测试代码,则所有测试都会失败,因为它们试图调用不再存在的方法,因为它们已被混淆器重命名。 我可以将某些方法标记为不混淆,以便它们可以由外部系统(例如我们的测试套件)使用,但由于我们正在追求高单元测试覆盖率,因此我们需要将所有方法标记为 un -可混淆。
如果我也混淆测试类,则会遇到两个问题:
1:生产类和测试类合并到同一输出目录中,并且我无法从生产 .jar 文件中排除测试类
2:我不能运行我正常的 Ant 批量测试调用:
<batchtest todir="${basedir}/reports">
<fileset dir="${basedir}/components/common/build-zkm">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
因为混淆器更改了测试的名称。
我可以在生成的 .war/.ear 文件上运行混淆器,但我想让我们的单元测试针对修改后的代码运行,以消除混淆器引起的任何错误。
我目前正在与 Zelix KlassMaster 合作,但我仍处于评估阶段,因此如果其他选择效果更好,我会愿意接受。
I am looking to obfuscate our Java web app code within our existing Ant build script, but am running into problems around unit testing. I am obfuscating the code right after it has been compiled, before it is jar-ed and before the unit tests are ran.
However, if I obfuscate my production code and not my test code, all my tests fail because they are trying to call methods that no longer exist because they have been renamed by the obfuscator. I can mark certain methods to not obfuscate so they can be used by external systems such as our test suite, but since we are shooting for high unit test coverage we will need to mark all of our methods as un-obfuscatable.
If I obfuscate the test classes as well, I run into two problems:
1: The production classes and the test classes get merged into the same output directory and I am unable to exclude the test classes from the production .jar files
2: I cannot run my normal Ant batchtest call:
<batchtest todir="${basedir}/reports">
<fileset dir="${basedir}/components/common/build-zkm">
<include name="**/*Test.class"/>
</fileset>
</batchtest>
because the obfuscator has changed the names of the tests.
I could just run the obfuscator on the resulting .war/.ear files, but I want to have our unit tests run against the modified code to drive out any bugs caused by the obfuscator.
I am currently working with Zelix KlassMaster, but I am still in the evaluation phase so I would be open to other options if they would work better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用 yguard (它是免费的,这就是我提到它的原因)。
您应该能够告诉混淆器不要混淆某些内容(看起来这里你可以)。
正如其他人所说,不要混淆测试,但要混淆其余部分。
但是,我建议您执行以下操作:
它会更慢,但如果如果测试在第 3 步失败,那么修复(可能)会更容易,如果测试在第 5 步失败,那么您就知道存在混淆问题,而不是源代码问题。
I use yguard (it is free, which is why I mention it).
You should be able to tell the obfuscator not to obfuscate certain things (looking here it seems you can).
Some as others have said, don't obfuscate the tests, but do obfuscate the rest.
However, I would suggest that you do the following:
It will be slower, but if the tests fail in step 3 it'll be easier to fix (potentially) and if the tests fail at 5 then you know there is an issue with the obfuscation not your source code.
您能否告诉它运行混淆器,以便它有效地重构代码包括测试中的引用(即当产品名称更改时,测试代码更改其引用)但不混淆测试本身(即不要更改测试类或其方法的名称)? 鉴于以前使用混淆器的经验,我希望它能起作用。
例如,假设我们有未混淆的源:
您想要设置混淆器的选项以使其有效:
这样您的“运行测试”Ant 任务应该能够保持不变,因为测试的 API 没有改变——只是方法的实现。
Can you tell it to run the obfuscator such that it effectively refactors the code including the references from the tests (i.e. when a production name changes, the test code changes its reference) but not to obfuscate the tests themselves (i.e. don't change the names of the test classes or their methods)? Given previous experience with obfuscators I'd expect that to work.
So for example, suppose we had unobfuscated source of:
You want to set the options of the obfuscator to make it effectively:
That way your "run the tests" Ant tasks should be able to stay the same, because the API of the tests hasn't changed - merely the implementation of the methods.
混淆器不应更改您的公共调用。 看来您应该在混淆之前运行其他测试,因为它们检查混淆后不应更改的内部功能。
因此,如果是这种情况,为什么不直接运行调用公共功能的测试呢? 您需要做的就是使用这些调用创建一个单独的类,并使用混淆的代码重新构建它,然后运行该 dll。
The obfuscator should not change your public calls. It seems that yo should run the other tests before obfuscation because they check internal functionality that should not change after obfuscation.
So if that is the case, why not just run the tests that call public functionality? All you need to do is have a separate class with those calls and re-build it using the obfuscated code and then run that dll.