Which method is used to mock static methods using PowerMock?

Which method is used to mock static methods using PowerMock?

A) mockStatic()
B) mock()
C) mockStaticMethods()
D) mockAllStatic()

Answer:

A) mockStatic()

Explanation:

The mockStatic() method in PowerMock is used to mock static methods. This allows you to replace the implementation of static methods with mock behavior during unit tests.

For example:


import static org.powermock.api.mockito.PowerMockito.*;

mockStatic(ClassWithStaticMethods.class);
when(ClassWithStaticMethods.staticMethod()).thenReturn("Mocked Value");

In this example, the mockStatic() method is used to mock the static method staticMethod() in the ClassWithStaticMethods class.

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