How do you mock a final method using PowerMock?

How do you mock a final method using PowerMock?

A) mock() and when()
B) spy() and doReturn()
C) mockFinalMethod()
D) mockFinal()

Answer:

A) mock() and when()

Explanation:

To mock a final method using PowerMock, you can use the mock() method in combination with when() to define the behavior of the final method.

For example:


ClassWithFinalMethod mock = mock(ClassWithFinalMethod.class);
when(mock.finalMethod()).thenReturn("Mocked Value");

In this example, the final method finalMethod() in the ClassWithFinalMethod class is mocked using PowerMock.

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