問題描述
我想從一個包含 2 個靜態方法 m1 和 m2 的類中模擬一個靜態方法 m1.我希望方法 m1 返回一個對象.
我嘗試了以下
1)
PowerMockito.mockStatic(Static.class, new Answer<Long>() {@覆蓋公共長答案(InvocationOnMock 調用)拋出 Throwable {返回 1000 升;}});
這同時調用了 m1 和 m2,它們具有不同的返回類型,因此它給出了返回類型不匹配錯誤.
2) PowerMockito.when(Static.m1(param1, param2)).thenReturn(1000l);
但是在執行 m1 時不會調用它.
3) PowerMockito.mockPartial(Static.class, "m1");
給出了 mockPartial 不可用的編譯器錯誤,這是我從 http://code.google.com 獲得的/p/powermock/wiki/MockitoUsage.
你想做的是1的一部分和2的全部組合.
您需要使用 PowerMockito.mockStatic 為類的所有靜態方法啟用靜態模擬.這意味著可以使用 when-thenReturn 語法對它們進行存根.
但是,您使用的 mockStatic 的 2 參數重載為 Mockito/PowerMock 在調用未在模擬實例上顯式存根的方法時應該執行的操作提供了默認策略.
來自 javadoc:p><塊引用>
創建具有指定策略的類模擬,以獲取其答案互動.這是相當高級的功能,通常你不需要它編寫體面的測試.但是,在使用時可能會有所幫助遺留系統.這是默認答案,因此僅在以下情況下使用你不會存根方法調用.
默認默認存根策略是只為對象、數字和布爾值方法返回 null、0 或 false.通過使用 2-arg 重載,您是在說不,不,不,默認情況下使用這個 Answer 子類的回答方法來獲取默認值.它返回一個 Long,所以如果你有靜態方法返回不兼容的東西長,有問題.
相反,使用 1-arg 版本的 mockStatic 來啟用靜態方法的存根,然后使用 when-thenReturn 指定對特定方法執行的操作.例如:
導入靜態 org.mockito.Mockito.*;導入 org.junit.Test;導入 org.junit.runner.RunWith;導入 org.mockito.invocation.InvocationOnMock;導入 org.mockito.stubbing.Answer;導入 org.powermock.api.mockito.PowerMockito;導入 org.powermock.core.classloader.annotations.PrepareForTest;導入 org.powermock.modules.junit4.PowerMockRunner;類 ClassWithStatics {公共靜態字符串 getString() {返回字符串";}公共靜態 int getInt() {返回 1;}}@RunWith(PowerMockRunner.class)@PrepareForTest(ClassWithStatics.class)公共類 StubJustOneStatic {@測試公共無效測試(){PowerMockito.mockStatic(ClassWithStatics.class);when(ClassWithStatics.getString()).thenReturn("你好!");System.out.println("字符串:" + ClassWithStatics.getString());System.out.println("Int:" + ClassWithStatics.getInt());}}
String 值靜態方法被存根返回Hello!",而 int 值靜態方法使用默認存根,返回 0.
I want to mock a static method m1 from a class which contains 2 static methods, m1 and m2. And I want the method m1 to return an object.
I tried the following
1)
PowerMockito.mockStatic(Static.class, new Answer<Long>() {
@Override
public Long answer(InvocationOnMock invocation) throws Throwable {
return 1000l;
}
});
This is calling both m1 and m2, which has a different return type, so it gives a return type mismatch error.
2) PowerMockito.when(Static.m1(param1, param2)).thenReturn(1000l);
But this is not called when m1 is executed.
3) PowerMockito.mockPartial(Static.class, "m1");
Gives compiler error that mockPartial not available, which I got from http://code.google.com/p/powermock/wiki/MockitoUsage.
What you want to do is a combination of part of 1 and all of 2.
You need to use the PowerMockito.mockStatic to enable static mocking for all static methods of a class. This means make it possible to stub them using the when-thenReturn syntax.
But the 2-argument overload of mockStatic you are using supplies a default strategy for what Mockito/PowerMock should do when you call a method you haven't explicitly stubbed on the mock instance.
From the javadoc:
Creates class mock with a specified strategy for its answers to interactions. It's quite advanced feature and typically you don't need it to write decent tests. However it can be helpful when working with legacy systems. It is the default answer so it will be used only when you don't stub the method call.
The default default stubbing strategy is to just return null, 0 or false for object, number and boolean valued methods. By using the 2-arg overload, you're saying "No, no, no, by default use this Answer subclass' answer method to get a default value. It returns a Long, so if you have static methods which return something incompatible with Long, there is a problem.
Instead, use the 1-arg version of mockStatic to enable stubbing of static methods, then use when-thenReturn to specify what to do for a particular method. For example:
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
class ClassWithStatics {
public static String getString() {
return "String";
}
public static int getInt() {
return 1;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassWithStatics.class)
public class StubJustOneStatic {
@Test
public void test() {
PowerMockito.mockStatic(ClassWithStatics.class);
when(ClassWithStatics.getString()).thenReturn("Hello!");
System.out.println("String: " + ClassWithStatics.getString());
System.out.println("Int: " + ClassWithStatics.getInt());
}
}
The String-valued static method is stubbed to return "Hello!", while the int-valued static method uses the default stubbing, returning 0.
這篇關于PowerMockito 模擬單個靜態方法并返回對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!