如何使用PowerMock-Mockito(No EasyMock)模拟默认构造函数?
我想通过这样做来访问对象的值。
例如:
Class A {
public A()
{
}
}
答案 0 :(得分:2)
PowerMockito.whenNew
API来执行此操作。有关详细信息,请参阅此链接:How to mock construction of new objects
答案 1 :(得分:0)
来自docs
@RunWith(PowerMockRunner.class)
@PrepareForTest(X.class)
public class XTest {
@Test
public void test() {
whenNew(MyClass.class).withNoArguments().thenThrow(new IOException("error message"));
X x = new X();
x.y(); // y is the method doing "new MyClass()"
..
}
}