问题
When I use PowerMock to mock the constructor,I want to specify the type of the paremeters. I use the method
PowerMock.expectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments)
If I want to specify the String
type, what shoud I fill in the parameterTypes
?
回答1:
Given these example classes:
public class MyClass {
public MyClass(String theParam) {
//Some interesting code...
}
}
public class MyFactory {
public MyClass createMyClass() {
return new MyClass("foo");
}
}
Then you would do an expectNew
as follows:
public class MyFactoryTest {
@Test
public void testCreateMyClass() {
//...
PowerMock.expectNew(MyClass.class, new Class[] {String.class}, "foo");
//...
}
}
来源:https://stackoverflow.com/questions/18480796/powermock-expectnew-how-to-specify-the-type-of-the-parameters