Centralize Mockito.mockStatic for entire test project

54 Views Asked by At

I removed entire PowerMock from the project because of its incompatibility with Java 17.

It is very huge project with lots of test cases and some important classes has static method which has been used in the project's test cases.

I replaced PowerMockito.mockStatic with Mocktio.mockstatic. Inheritance is getting used. Project has important ParentTest class and all rest of test extends it.

I moved Mocktio.mockstatic from the child class's to Parent class with @Before and closed this mockstatic @After.

By doing all this, things are working but over all test coverage has been dropped drastically with reason of 'static mocking is already registered in the current thread'

Also some test cases runs individually but fails when run with the Test Suite with same reason.

Below one

 @RunWith(MockitoJUnitRunner.Silent.class)
@ContextConfiguration(locations ="classpath:test-setup.xml"
        
})

public class ParentTest extends TestCase{

     private MockedStatic<HDType> mockStaticHDType;

     @Before
     public void intialization() throws Exception {

         mockStaticHDType = Mockito.mockStatic(HDType.class);


     

    @After
    public void close(){
    mockStaticHDType.close();
        
    }
}


public class ABC extends ParentTest {

     @Test
     public void testHD() throws BusinessException {

                Hd hd = Mockito.mock(Hd.class);
        hd.setHDId(1);
            Mockito.when(HDType.getHDimension(Mockito.any(),Mockito.any())).thenReturn(hd);
           
  }

}

The above approach of centralizing the Mockito.mockstatic for entire project is correct one? or does it need to create it in each individual test class and to close it there.

If there is any other good approach then please do help me.

0

There are 0 best solutions below