Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throws InvocationTargetException while calling static method #1132

Open
hemant-karwade opened this issue May 27, 2022 · 0 comments
Open

Throws InvocationTargetException while calling static method #1132

hemant-karwade opened this issue May 27, 2022 · 0 comments

Comments

@hemant-karwade
Copy link

Hi,

I am trying to mock some classes from LaunchDarkly JAVA Client library.

I have created following class with constructor, which I like to unit test by mocking some of

public class SampleClass { 
.........................
........................
public SampleClass(String strBaseUrl, String strProjKey, String strEnv, String strAuthToken) {
        ................................
        ...................
        **this.defaultClient = Configuration.getDefaultApiClient();**
        ,,,,,,,,,,,,,,,,,,,,,,,,
        .......................
        this.featureFlagsApiInstance = new FeatureFlagsApi(defaultClient);
        this.userApiInstance = new UsersApi(defaultClient);
    }
}

When I call this constructor in test class using PowerMock getting exception InvocationTargetException and get null instance.
TestClass:

@RunWith(PowerMockRunner.class)
@PrepareForTest(fullyQualifiedNames = "com.trmsys.tacoe.launchdarkly.utils.*")
@PowerMockIgnore("jdk.internal.reflect.*")
public class LDUtilTest2 {

  @Test
  public void testLdUtils() {
    //add behaviour of LD client library
   try{
      FeatureFlagsApi featureFlagsApiInstance = mock(FeatureFlagsApi.class);
      UsersApi userApiInstance = mock(UsersApi.class);
      **LDHelper ldHelper = new LDHelper("string", "string", "string", "string");**
      ....................................
      ...................................
    }
   catch(Exception e) {
      e.printStackTrace();
      Assert.fail();
   }
 }

Observations: From SampleClass statement "this.defaultClient = Configuration.getDefaultApiClient();" which is static method, throw this exception which cause this test failure... I tried mocking this static method too by adding following statements in test class, but result is same

   PowerMockito.mockStatic(Configuration.class);
   PowerMockito.when(Configuration.getDefaultApiClient()).thenReturn(new ApiClient());

Java module and their versions being used:

Java: 11
junit: 4.12
mockito-all: 1.10.19
powermock-api-mockito: 1.7.0
powermock-module-junit4: 1.7.0

Best Regard
Hemant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant