Expand my Community achievements bar.

SOLVED

How to Mock Static method of Util class in Service Implementation

Avatar

Employee

Hi Team,

 

I have a git repository(Repo A) which has dependent on other main repository (Repo B). In Repo A we have a service implementation. In Repo B we have common code, here we have a util class and it has a static methods. We are calling these static methods from Repo B in Repo A. Now we are writing the Junit implementation for service class in Repo A. I am trying to mock this static method as mentioned below. Mockito.mockStatic(Utils.class); Mockito.when(Utils.getString()).thenReturn("Mocked string"); . i have added dependencies in my Repo A core POM and parent POM files.

 

I am facing issue i am not able to use the static method. I am seeing error "Cannot resolve method 'mockStatic' in 'Mockito'". Can someone help me on this issue. Your help is much appreciated.

 

https://sourcedcode.com/blog/aem/junit/junit5/mocking-static-methods-in-junit5 

 

 

 

Thanks,

Praveena.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

You can check the below community post
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-for-accesscontroluti... 

Additionally, you can explore the GitHub commit where we've mocked the HttpClient.newBuilder static method. https://github.com/MahediSabuj/aem-demo/commit/677d316157b98757490d59fa723ea77602127405 

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

You can check the below community post
https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-for-accesscontroluti... 

Additionally, you can explore the GitHub commit where we've mocked the HttpClient.newBuilder static method. https://github.com/MahediSabuj/aem-demo/commit/677d316157b98757490d59fa723ea77602127405 

Avatar

Community Advisor

Hi @PraveenaTh , Please make sure you are using latest version of Junit and mockito dependencies.
MockedStatic was introduced in mockito 3.4.3. If your mockito version is previous version of that it won't work. 

Please try with below,

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.9.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit-addons</groupId>
<artifactId>junit-addons</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>

Avatar

Community Advisor

Hi, 

The problem is that in some Mockito versions, the mockStatic method was disabled by default. There are a couple of ways to configure this, but I think the easiest one is just to use the mockito-inline dependency instead of mockito-core. Try something like this:

      <dependency>               
        <groupId>org.mockito</groupId>
        <artifactId>mockito-inline</artifactId>             
        <version>5.2.0</version>              
        <scope>test</scope>       
      </dependency>

 

References: 

https://wttech.blog/blog/2020/mocking-static-methods-made-possible-in-mockito-3.4.0/

https://github.com/mockito/mockito/issues/2528

 

Hope this helps



Esteban Bustamante

Avatar

Administrator

@PraveenaTh Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!  



Kautuk Sahni