Yus*_*zal 6 java junit powermockito
我需要模拟一个类的静态方法并在我的测试中使用该模拟方法。现在看来我只能使用 PowerMock 来做到这一点。
我用@RunWith(PowerMockRunner.class) 和@PrepareForTest 用适当的类注释该类。
在我的测试中,我有一个 @ClassRule,但是在运行测试时,该规则应用不正确。
我能做什么 ?
RunWith(PowerMockRunner.class)
@PowerMockIgnore({
"javax.xml.*",
"org.xml.*",
"org.w3c.*",
"javax.management.*"
})
@PrepareForTest(Request.class)
public class RoleTest {
@ClassRule
public static HibernateSessionRule sessionRule = new HibernateSessionRule(); // this Rule doesnt applied
Run Code Online (Sandbox Code Playgroud)
解决此问题的另一种方法是使用org.powermock.modules.junit4.PowerMockRunnerDelegate注释:
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(BlockJUnit4ClassRunner.class)
@PowerMockIgnore({
"javax.xml.*",
"org.xml.*",
"org.w3c.*",
"javax.management.*"
})
@PrepareForTest(Request.class)
public class RoleTest {
@ClassRule
public static HibernateSessionRule sessionRule = new HibernateSessionRule(); // this Rule now applied
Run Code Online (Sandbox Code Playgroud)
我查看了 PowerMock 代码。貌似PowerMockRunner不支持@ClassRule。您可以尝试使用 theHibernateSessionRule作为 a@Rule而不是 a @ClassRule。
@PrepareForTest(Request.class)
public class RoleTest {
@Rule
public HibernateSessionRule sessionRule = new HibernateSessionRule();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1828 次 |
| 最近记录: |