Junit 参数化测试与 Powermock 一起 - 如何?
我一直在试图弄清楚如何在 Junit4 中与 PowerMock 一起运行参数化测试。问题是,要使用 PowerMock,您需要用 来装饰您的测试类,
@RunWith(PowerMockRunner.class)
而要使用参数化测试,您必须用 用来装饰
@RunWith(Parameterized.class)
从我看来,它们似乎是相互排斥的!?这是真的吗?有什么办法解决这个问题吗?我尝试在使用 PowerMock 运行的类中创建参数化类;像这样的东西:
@RunWith(PowerMockRunner.class)
class MyTestClass {
@RunWith(Parameterized.class)
class ParamTestClass {
// Yadayada
}
}
但不幸的是,这并没有多大好处... ParamTestClass
仍然无法在 PowerMock 支持下运行(也许并不令人惊讶)...而且我已经用完了的想法,因此非常感谢任何帮助!
更新: 对于未来的 Google 用户,另请参阅:使用PowerMock 没有 RunWith?
I've been trying to figure out how to run parameterized tests in Junit4 together with PowerMock. The problem is that to use PowerMock you need to decorate your test class with
@RunWith(PowerMockRunner.class)
and to use parameterized tests you have to decorate with
@RunWith(Parameterized.class)
From what I can see they seem mutually excluded!? Is this true? Is there any way around this? I've tried to create a parameterized class within a class running with PowerMock; something like this:
@RunWith(PowerMockRunner.class)
class MyTestClass {
@RunWith(Parameterized.class)
class ParamTestClass {
// Yadayada
}
}
But unfortunately this doesn't do much good... The ParamTestClass
still doesn't run with PowerMock support (not that surprisingly maybe)... And I've kind of run out of ideas so any help is greatly appreciated!
Update:
For future googlers also see: Using PowerMock without the RunWith?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我有同样的问题。不幸的是,由于我拥有的 JVM,它不允许我使用 PowerMock 规则。我使用 RunnerDelegate 代替规则。
I had the same issue. Unfortunately it would not let me use a PowerMock Rule due to the JVM I had. Instead of the rule I used RunnerDelegate.
是的,如果您使用 JUnit 4.7+,则可以使用 PowerMock 规则 来实现。
Yes this works by using the PowerMock Rule available if you use JUnit 4.7+.
以下解决方案对我有用!
The following solution worked for me!
您不能同时使用两个类运行程序,因此您肯定必须编写自己的测试运行程序才能实现这一点。
我对 Powermock 一无所知,但经过 10 秒的研究,看起来一个解决方案是编写一个使用 powermock 的类加载器并运行参数化测试的测试运行程序。如果您可以弄清楚如何从自定义测试运行程序中委托给参数化测试运行程序,那么这可能是您最好的选择。
You can't use two class runners at once, so you will definitely have to write your own test runner to make that happen.
I don't know anything about Powermock, but after 10 seconds of research, it looks like one solution would be to write a test runner which uses powermock's class loader and runs parameterized tests. If you can figure out how to delegate to the parameterized test runner from within your custom test runner, that might be your best bet.