How do you mock a private method using PowerMock?
A) spy() and when()
B) mock() and verify()
C) mockPrivate() and thenReturn()
D) spy() and mockPrivate()
Answer:
A) spy() and when()
Explanation:
To mock a private method using PowerMock, you first use spy()
to create a partial mock of the object, and then use when()
to define the behavior of the private method.
For example:
MyClass myClass = spy(new MyClass());
when(myClass, "privateMethodName").thenReturn("Mocked Value");
In this example, spy()
is used to create a partial mock of MyClass
, and when()
is used to mock the private method privateMethodName
.