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

发布于 2024-08-26 02:45:45 字数 836 浏览 6 评论 0原文

我一直在试图弄清楚如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两相知 2024-09-02 02:45:45

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

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

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.

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Parameterized.class)
老娘不死你永远是小三 2024-09-02 02:45:45

是的,如果您使用 JUnit 4.7+,则可以使用 PowerMock 规则 来实现。

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

君勿笑 2024-09-02 02:45:45

以下解决方案对我有用!

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

The following solution worked for me!

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(Parameterized.class)
空袭的梦i 2024-09-02 02:45:45

您不能同时使用两个类运行程序,因此您肯定必须编写自己的测试运行程序才能实现这一点。

我对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文