Using PowerMockito.whenNew() is not getting mocked and original method is called

You need to put the class where the constructor is called into the @PrepareForTest annotation instead of the class which is being constructed – see Mock construction of new objects.

In your case:

@PrepareForTest(MyQueryClass.class)

@PrepareForTest(A.class)

More general:

@PrepareForTest(NewInstanceClass.class)

@PrepareForTest(ClassThatCreatesTheNewInstance.class)

Leave a Comment