Ich schreibe einen Test mit Junit + Mockito + Powermock.Kann ich statische Methoden nur mit powermock + mockito + junit ausprobieren?
Ich habe eine Klasse wie folgt, die ich testen wollen:
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.
}
Ich will nur method1
aber nicht method2
oder method3
verspotten.
@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
}
...
}
Kann ich einige Methoden verspottet und einige nicht spotten das heißt, sie während des Tests ihre ursprüngliche Implementierung halten? Ist das mit Mockito + Powermock möglich?
Mein Test sieht vielleicht nicht sehr elegant aus, aber ich habe meinen Anwendungsfall vor dem Posten hier vereinfacht.
Vielen Dank.
Mögliches Duplikat von [Mock eine einzelne statische Methode mit PowerMock und TestNG] (https://stackoverflow.com/questions/20398120/mock-a-single-static-method-using-powerermock-and-testng) – Turing85