0

I am using ConfigProvider.getConfig().getValue("myproject.some.remote.service.url", String.class); from org.eclipse.microprofile.config . when iam trying to mock the ConfigProvider class getting org.mockito.exceptions.base.MockitoException cannot mock/spy org.eclipse.microprofile.config.ConfigProvider. Need help in resolving the issue.

  • You are trying to mock a final class. You need to use the inline mock maker. Check the docs: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#39 – Lesiak Dec 07 '21 at 17:52
  • Does this answer your question? [How to mock a final class with mockito](https://stackoverflow.com/questions/14292863/how-to-mock-a-final-class-with-mockito) – Lesiak Dec 07 '21 at 18:05

1 Answers1

1

we can mock ConfigProvider using mockito library by following the ways

public void testMethodWithMockConfigProvider(){ 
    Config config= mock(Config.class);
    try (MockedStatic<ConfigProvider> mockConfigProvider = Mockito.mockStatic(ConfigProvider.class)) {
        mockConfigProvider.when(()-> ConfigProvider.getConfig()).thenReturn(config);
        when(config.getValue(eq("myproject.some.remote.service.url"), any())).thenReturn("mockUrl");

}//try with resource close
}//end of method

Sometimes there may be needed to add dependencies for static methods. they are junit5-mockito and mockito-inline

Ramgau
  • 420
  • 2
  • 6
  • 16