Reputation: 6872
Mockito 3.6 supports mocking static methods under a try-with-resources
block as explained here.
Can someone let me know if static methods are mocked using Powermock in @Before
or @BeforeClass
can Mockito.mockStatic
be used to replace them without an entire rewrite of the test class?
Upvotes: 10
Views: 11642
Reputation: 422
I think you might need to do a little bit of refactoring. You can create mocks of static methods by creating a MockedStatic variable at class level and use that in your tests and also sometimes it needs to be closed in the @After block, something like
MockedStatic<StaticClass> mockedStaticClass;
@Before
public void setUp()
{
mockedStaticClass = Mockito.mockStatic(StaticClass.class);
}
@After
public void tearDown() throws Exception
{
mockedStaticClass.close();
}
@Test
public void yourTest()
{
//make use of mockedStatic variable you created earlier
}
Upvotes: 19