Mock Static Methods with Mockito
Published
Utilizing PowerMockito with Mockito < v3.4.0
pom.xml
<properties>
<powermock.version>2.0.9</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
</dependencies>
HttpClient
, commonly used for invoking external APIs, as an example to write unit test using PowerMockito.RestClientServiceImpl.java
public HttpClient getHttpClient(int... timeout) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
int connectionTimeout = Arrays.stream(timeout).findFirst()
.orElse(CONNECTION_TIMEOUT);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(connectionTimeout * 1000)
.setConnectionRequestTimeout(connectionTimeout * 1000)
.setSocketTimeout(connectionTimeout * 1000).build();
httpClientBuilder.setDefaultRequestConfig(config);
return httpClientBuilder.build();
}
RestClientServieimplTest.java
public class RestClientServiceImplTest {
CloseableHttpClient httpClient;
HttpClientBuilder httpClientBuilder;
RestClientServiceImpl restClient;
public void setup() {
MockitoAnnotations.initMocks(this);
PowerMockito.mockStatic(HttpClientBuilder.class);
Mockito.when(HttpClientBuilder.create()).thenReturn(httpClientBuilder);
Mockito.when(httpClientBuilder.build()).thenReturn(httpClient);
}
public void getHttpClientTest() {
Assert.assertNotNull(restClient);
HttpClient httpClient = restClient.getHttpClient();
Assert.assertNotNull(httpClient);
}
}
Utilizing Mockito-Inline with Mockito < v5.0.0
pom.xml
<properties>
<junit5.version>5.4.0</junit5.version>
<mockito.version>3.4.0</mockito.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit5.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
RestClientService
from previous example can be achieved using Mockito-Inline as shown below.RestClientServieimplTest.java
public class RestClientServiceImplTest {
CloseableHttpClient httpClient;
HttpClientBuilder httpClientBuilder;
RestClientServiceImpl restClient;
public void setup() {
MockitoAnnotations.initMocks(this);
}
public void getHttpClientTest() {
try(MockedStatic<HttpClientBuilder> mocked = Mockito.mockStatic(HttpClientBuilder.class)) {
httpClientMockedStatic.when(HttpClientBuilder::create).thenReturn(httpClientBuilder);
Mockito.when(httpClientBuilder.build()).thenReturn(httpClient);
Assertions.assertNotNull(restClient);
HttpClient httpClient = restClient.getHttpClient();
Assertions.assertNotNull(httpClient);
}
}
}