问题
How should I mock the default constructor using PowerMock-Mockito(No EasyMock) ?
I want to access the values of the object by doing this.
For example :
Class A {
public A()
{
}
}
回答1:
PowerMockito.whenNew
API should be used to perform this. See this link for further information: How to mock construction of new objects
回答2:
from 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()"
..
}
}
来源:https://stackoverflow.com/questions/15501572/how-to-mock-the-default-constructor-using-powermock-with-mockito