私はJunit + Mockito + Powermockを使ってテストを書いています。いくつかの静的メソッドは、powermock + mockito + junitを使用してのみモックできますか?
public class MyUtils {
public static Object method1() {} //I want to mock this only
public static void method2() {} //I want to keep this as is during my test.
public static void method3() {} //I want to keep this as is during my test.
}
は私だけではなく、method1
method2
またはmethod3
をモックとしたい:
は、私がテストしたい、次のようなクラスを持っています。
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyUtils.class)
public class MyTest {
@Before
public void setUpBeforeClass() throws Exception {
PowerMockito.mockStatic(MyUtils.class);
}
@Test
public void test1() throws Exception {
when(MyUtils.method1()).thenReturn(something);
MyUtils.method3(); //method3 is getting mocked with an empty implementation by PowerMockito
}
...
}
は、私はいくつかの嘲笑のメソッドといくつかを持つことができ、すなわち、彼らはテスト中に、元の実装を保つ嘲笑されていませんか?これはMockito + Powermockで可能ですか?
私のテストは非常にエレガントに見えないかもしれませんが、ここに投稿する前に私のユースケースを簡略化しました。
ありがとうございます。
[PowerMockとTestNGを使用して1つの静的メソッドをモックする]の可能な複製(https://stackoverflow.com/questions/20398120/mock-a-single-static-method-using-powermock-and-testng) – Turing85