存在@rule和powermockrule问题

7uzetpgm  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(497)

我正在使用

@Rule
public PowerMockRule rule = new PowerMockRule();

在我的一个测试文件里。现在,当我删除这部分代码时,几个测试用例开始失败,它们位于不同的文件夹中,并且与此文件没有直接关系。有什么问题吗?

7eumitmz

7eumitmz1#

有关详细信息,请参阅下面的wiki链接。
从技术上讲,不是使用 @RunWith(PowerMockRunner.class) ,您可以使用 PowerMockRule 如下例所示。
所以,当你移除 PowerMockRule 你的测试会失败因为你没有 @RunWith(PowerMockRunner.class) 在你的测试中。
https://github.com/powermock/powermock/wiki/powermockrule
使用junit规则引导
功能自powermock 1.4版本1.4起可用—可以使用junit规则引导powermock,而不是使用powermockrunner和runwith注解。这允许您使用其他junit运行程序,同时仍然受益于powermock的功能。为此,可以指定:
PowerMockRule ```
@PrepareForTest(X.class);
public class MyTest {
@Rule
PowerMockRule rule = new PowerMockRule();
// Tests goes here
...
}

没有 `PowerMockRule` ```
@RunWith(PowerMockRunner.class)
@PrepareForTest(X.class); 
public class MyTest {

     // Tests goes here
     ... 
}

相关问题