What is the purpose of the @RunWith(PowerMockRunner.class) annotation?
A) To run tests with PowerMock’s custom runner
B) To create mock objects
C) To log test results
D) To set up the database for testing
Answer:
A) To run tests with PowerMock’s custom runner
Explanation:
The @RunWith(PowerMockRunner.class)
annotation is used to run tests with PowerMock’s custom runner. This runner is necessary for enabling PowerMock’s advanced features, such as mocking static methods, constructors, and final methods.
For example:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassWithStaticMethods.class)
public class MyTest {
// Test methods here
}
In this example, the @RunWith(PowerMockRunner.class)
annotation is used to run the test class with PowerMock’s runner, allowing it to mock static methods in ClassWithStaticMethods
.