Home > Blockchain >  Getting a Static mocking is already registered in the current thread Exception
Getting a Static mocking is already registered in the current thread Exception

Time:09-30

I have this following test case

@Test
public void test() throws IOException {
    try (MockedStatic<AWSStepFunctionsClientBuilder> awsMock = Mockito.mockStatic(AWSStepFunctionsClientBuilder.class, Mockito.RETURNS_DEEP_STUBS)) {
        AWSStepFunctions awsStepFunctionsMock = mock(AWSStepFunctions.class);
        awsMock.when(() -> AWSStepFunctionsClientBuilder.standard().withClientConfiguration(Mockito.any()).build()).thenReturn(awsStepFunctionsMock);

        MockedStatic<HttpClients> httpClientsMockedStatic = Mockito.mockStatic(Mockito.RETURNS_DEEP_STUBS);
        httpClientsMockedStatic.when( () -> HttpClients.custom().setRetryHandler.build()).thenReturn(httpClientMock);
           
    }
}

I a running multiple tests in this class with the same objects, individually then run fine, but when I run all of them at once, I get org.mockito.exceptions.base.MockitoException: For org.apache.http.impl.client.HttpClients, static mocking is already registered in the current thread. To create a new mock, the existing static mocking must be deregistered.

I there another way I can configure the httpClientsMockedStatic, or is there a way to resolve this? I tried using different variable names for each of them but that doesn't work. Also tried close() on them after the test, and also using closeOnDemand, but that doesn't seem to work.

I also have to use PowerMockito for new object creation, as there are many new objects created in the classes I test that I cannot modify. If there's a way to do this via Mockito, without a try-with-resources for multiple objects and their behavior, that would work for me as well.

Thanks in advance!

CodePudding user response:

Here is how I solved this. I do have to preface this that I need to use this with a peculiar combination of Junit4, PowerMockito, and Mockito 4.0.0.

I was searching for various ways to configure the MockitoSettings to close the instances/threads created, but it turns out I can just configure them as I would configure other Mocks in the @BeforeClass setUp method.

public ClassTest {

    private static CloseableHttpClient httpClientMock;
    private static MockedStatic<HttpClients> httpClientsMockedStatic;
    
    @BeforeClass
    public static void setUp() throws IOException {
        httpClientMock = mock(CloseableHttpClient.class);
        httpClientsMockedStatic = Mockito.mockStatic(HttpClients.class, Mockito.RETURNS_DEEP_STUBS);
        httpClientsMockedStatic.when( () -> HttpClients.custom().setRetryHandler.build()).thenReturn(httpClientMock); 
        // You can also configure the behavior of above line in whatever test method you would like

}

You can also do a .close on the variable that is a static mock in an @After method, like for the above example it would be httpClientsMockedStatic.close()

  • Related