[英]Mock static methods from multiple class using PowerMock
我知道如何使用 PowerMock 從類中模擬靜態方法。
但是我想使用 JUnit 和 PowerMock 在一個測試類中模擬來自多個類的靜態方法。
誰能告訴我是否有可能做到這一點以及如何做到這一點?
只需為多個類執行@PrepareForTest({Class1.class,Class2.class})
。
@Test
@PrepareForTest({Class1.class, Class2.class})
public final void handleScript() throws Exception {
PowerMockito.mockStatic(Class1.class);
PowerMockito.mockStatic(Class2.class);
等等...
在帶有 powermock/junit 的 java 中,將@PrepareForTest({})
與數組 ( {}
) 一樣多的靜態類使用。
@RunWith(PowerMockRunner.class)
@PrepareForTest({XmlConverterA.class, XmlConverterB.class})
class TransfersServiceExceptionSpec {
}
我在 Scala/junit 中使用了powermock,因為 Scalatest 沒有與 powermock 集成。
@RunWith(classOf[PowerMockRunner])
@PrepareForTest(Array(classOf[XmlConverterA], classOf[XmlConverterB]))
class TransfersServiceExceptionSpec {
@Test
def test() {
}
}
如果您使用的是 kotlin,則語法是這樣的
@PrepareForTest(ClassA::class, ClassB::class)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.