@RunWith(PowerMockRunner.class)
@PrepareForTest(Helper.class)//1.新增要初始化的類,就是建構函式所在的類
public class HelperTest {
@Mock
private Something mockSomething;
@InjectMocks
private Helper helper;
@Test
public void doSomething() throws Exception {
String argument = "arg";
PowerMockito.whenNew(Something.class).withArguments(argument).thenReturn(mockSomething);//2.新增要構造的函式的類,引數和返回值
// 3.呼叫需要測試方法
helper.doSomething(argument);
// 進行驗證
verify(mockSomething).doIt();
}
public class Helper {
public void doSomething(String arg) {
Something something = new Something(arg);
something.doit();
@RunWith(PowerMockRunner.class)
@PrepareForTest(Helper.class)//1.新增要初始化的類,就是建構函式所在的類
public class HelperTest {
@Mock
private Something mockSomething;
@InjectMocks
private Helper helper;
@Test
public void doSomething() throws Exception {
String argument = "arg";
PowerMockito.whenNew(Something.class).withArguments(argument).thenReturn(mockSomething);//2.新增要構造的函式的類,引數和返回值
// 3.呼叫需要測試方法
helper.doSomething(argument);
// 進行驗證
verify(mockSomething).doIt();
}
}
public class Helper {
public void doSomething(String arg) {
Something something = new Something(arg);
something.doit();
}
}