クラスの静的メソッドをモックするように設定しています。これは、注釈付きの@Before
JUnitセットアップメソッドで行う必要があります。
私の目標は、明示的にモックするメソッドを除いて、実際のメソッドを呼び出すようにクラスを設定することです。
基本的に:
@Before
public void setupStaticUtil() {
PowerMockito.mockStatic(StaticUtilClass.class);
// mock out certain methods...
when(StaticUtilClass.someStaticMethod(anyString())).thenReturn(5);
// Now have all OTHER methods call the real implementation??? How do I do this?
}
私が遭遇している問題はStaticUtilClass
、メソッド内で、public static int someStaticMethod(String s)
残念ながら、値RuntimeException
が指定されている場合にaをスローすることです。null
したがって、以下のように、デフォルトの答えとして実際のメソッドを呼び出すという明白なルートを単純に実行することはできません。
@Before
public void setupStaticUtil() {
PowerMockito.mockStatic(StaticUtilClass.class, CALLS_REAL_METHODS); // Default to calling real static methods
// The below call to someStaticMethod() will throw a RuntimeException, as the arg is null!
// Even though I don't actually want to call the method, I just want to setup a mock result
when(StaticUtilClass.someStaticMethod(antString())).thenReturn(5);
}
モックに関心のあるメソッドの結果をモックした後、他のすべての静的メソッドで実際のメソッドを呼び出すようにデフォルトの回答を設定する必要があります。
これは可能ですか?