2016-08-04 37 views
2

我需要模擬一個類的靜態方法,並在我的測試中使用該模擬方法。現在看來,我只能使用PowerMock來做到這一點。PowerMockRunner不適用JUnit ClassRules

我使用相應的類爲@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 
+0

只用於追蹤:https://github.com/powermock/powermock/issues/687。 PowerMock基本上打破了JUnit測試處理,並沒有真正的解決方案。 –

回答

0

我看着PowerMock代碼。它看起來像PowerMockRunner不支持@ClassRule。您可以嘗試使用HibernateSessionRule作爲@Rule而不是@ClassRule

@PrepareForTest(Request.class) 
public class RoleTest { 

    @Rule 
    public HibernateSessionRule sessionRule = new HibernateSessionRule(); 
+0

壞消息:(但謝謝你的答案。 –