I know how to mock static methods from a class using PowerMock.
But I want to mock static methods from multiple classes in a test class using JUnit and PowerMock.
Can anyone tell me is it possible to do this and how to do it?
Step 1: Create a class that contains a private method. We have created class with the name Utility and defined a private method and a public method (that returns the object of private method). Step 2: Create a JUnit test case named PowerMock_test for testing purpose.
Since static method belongs to the class, there is no way in Mockito to mock static methods. However, we can use PowerMock along with Mockito framework to mock static methods.
Power Mock gives you access to mock static methods, constructors etc. and this means that your code is not following best programming principles. Power Mock should be used in legacy applications where you cannot change the code which has been given to you.
Just do @PrepareForTest({Class1.class,Class2.class})
for multiple classes.
@Test @PrepareForTest({Class1.class, Class2.class}) public final void handleScript() throws Exception { PowerMockito.mockStatic(Class1.class); PowerMockito.mockStatic(Class2.class);
etc...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With