与普通的JUnit相比,Powermock JUnit测试花费了更多的时间来执行 [英] Powermock JUnit tests are taking more time to execute compared to normal JUnit

查看:81
本文介绍了与普通的JUnit相比,Powermock JUnit测试花费了更多的时间来执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用powermock通过使用@PrepareForTest批注来模拟静态方法.测试运行正常,但问题是执行测试需要更多时间.

we are using powermock for mocking the static methods by using the @PrepareForTest annotations. The test runs fine but the problem is it is taking more time to execute the test.

代码段如下:

@PrepareForTest({ StaticClass1.class, StaticClass2.class })

通常,不模拟静态方法的JUnit执行大约需要2秒的时间,但是当我们添加@PrepareForTest注释以模拟静态调用时,测试将花费大约一分钟的时间来完成执行.对于一个测试来说,这并不是什么大的影响,但是我们进行了超过1K的测试,整个构建时间花费了2个小时以上,而之前花费了大约20-30分钟.

Normally, the JUnit with out mocking the static method is taking around 2 sec time to execute but when we add @PrepareForTest annotation for mocking the static calls, the test is taking around a minute time to complete the execution. For one test, this is not a big impact, but we have more than 1K tests and the overall build time is taking more than 2 hrs which earlier took around 20 - 30 minutes.

电源模拟程序花费这么多时间的原因可能是什么.

任何帮助将不胜感激.

推荐答案

PowerMock是一种工具,可以对生成的类执行字节码检测.将您的类编译成Java字节码后,PowerMock就会出现并修改此生成的类.否则(使用Java代理)不可能模拟私有方法或静态方法.

PowerMock is a tool that performs bytecode instrumentation on your generated classes. Once your classes are compiled into Java Bytecode, PowerMock comes along and modifies this generated classes. It is not possible otherwise (by using Java Proxy) to mock a private or a static method.

仅应在想要使用PowerMock专有功能的类中使用@PrepareForTest批注,例如模拟私有方法或静态方法.

You should only use @PrepareForTest annotation only in those classes that you want to use features exclusive to PowerMock, such as mocking private or static methods.

这篇关于与普通的JUnit相比,Powermock JUnit测试花费了更多的时间来执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆