How do you mock a constructor using PowerMock?

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.

Reference links:

https://www.javaguides.net/p/top-java-libraries.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top