@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();
}
}
public void doSomething(String arg) {
Something something = new Something(arg);
something.doit();
}
}