How do you mock a constructor using PowerMock?
A) whenNew()
B) mockConstructor()
C) mockNew()
D) mockObject()
Answer:
A) whenNew()
Explanation:
The whenNew()
method in PowerMock is used to mock a constructor. It allows you to define the behavior of a new instance creation without actually invoking the constructor.
For example:
whenNew(ClassToBeMocked.class).withAnyArguments().thenReturn(mock(ClassToBeMocked.class));
In this example, the whenNew()
method is used to mock the constructor of ClassToBeMocked
, so that a mock object is returned whenever a new instance is created.