java Junit 参数化测试与 Powermock 一起 - 如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2454473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:14:21  来源:igfitidea点击:

Junit Parameterized tests together with Powermock - how?

javajunitpowermockparameterized

提问by Anders Hansson

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

我一直试图弄清楚如何在 Junit4 中与 PowerMock 一起运行参数化测试。问题是要使用 PowerMock,您需要用

@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:

从我所见,它们似乎相互排斥!?这是真的?有没有办法解决?我尝试在使用 PowerMock 运行的类中创建参数化类;像这样:

@RunWith(PowerMockRunner.class)
class MyTestClass {
     @RunWith(Parameterized.class)
     class ParamTestClass {
          // Yadayada
     }
}

But unfortunately this doesn't do much good... The ParamTestClassstill 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!

但不幸的是,这并没有多大好处......ParamTestClass仍然不能在 PowerMock 支持下运行(也许并不奇怪)......而且我的想法已经用完了,所以非常感谢任何帮助!

Update:For future googlers also see: Using PowerMock without the RunWith?

更新:对于未来的 googlers 还可以看到:在没有 RunWith 的情况下使用 PowerMock?

采纳答案by Abercrombieande

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.

我遇到过同样的问题。不幸的是,由于我拥有的 JVM,它不允许我使用 PowerMock 规则。我使用 RunnerDelegate 代替了规则。

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Parameterized.class)

回答by Johan

Yes this works by using the PowerMock Ruleavailable if you use JUnit 4.7+.

是的,如果您使用 JUnit 4.7+,这可以通过使用可用的 PowerMock规则来工作。

回答by Rohit Jaiswal

The following solution worked for me!

以下解决方案对我有用!

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Parameterized.class)

回答by Sean Reilly

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.

我对 Powermock 一无所知,但经过 10 秒的研究,看起来一个解决方案是编写一个测试运行程序,它使用 powermock 的类加载器并运行参数化测试。如果您能弄清楚如何从自定义测试运行器中委托给参数化测试运行器,那可能是您最好的选择。