Mock static methods from multiple class using PowerMock
Just do @PrepareForTest({Class1.class,Class2.class}) for multiple classes.
Just do @PrepareForTest({Class1.class,Class2.class}) for multiple classes.
What you want to do is a combination of part of 1 and all of 2. You need to use the PowerMockito.mockStatic to enable static mocking for all static methods of a class. This means make it possible to stub them using the when-thenReturn syntax. But the 2-argument overload of mockStatic you are using supplies … Read more
You need to put the class where the constructor is called into the @PrepareForTest annotation instead of the class which is being constructed – see Mock construction of new objects. In your case: ✗ @PrepareForTest(MyQueryClass.class) ✓ @PrepareForTest(A.class) More general: ✗ @PrepareForTest(NewInstanceClass.class) ✓ @PrepareForTest(ClassThatCreatesTheNewInstance.class)
Try adding this annotation to your Test class: @PowerMockIgnore(“javax.management.*”) Worked for me.