You are currently viewing PowerMock vs. WireMock vs. Mockito

PowerMock vs. WireMock vs. Mockito

  • Post author:
  • Post category:Java
  • Post comments:0 Comments
  • Post last modified:February 24, 2024

In the world of Java testing, frameworks like PowerMock, WireMock, and Mockito play significant roles. They each serve different purposes but are often used together to achieve comprehensive testing for Java applications. In this tutorial, we’ll explore these frameworks, their purposes, and provide examples of how to use them.

1. Mockito


  • Mockito is a popular mocking framework that simplifies the creation, configuration, and verification of mocks in unit tests.
  • It is primarily used for creating mock objects of interfaces and classes.
  • Mockito helps in isolating the class under test by mocking its dependencies.


Let’s say we have a simple Calculator interface and a CalculatorService class that uses this interface:

public interface Calculator {
    int add(int a, int b);
    int subtract(int a, int b);

public class CalculatorService {
    private Calculator calculator;

    public CalculatorService(Calculator calculator) {
        this.calculator = calculator;

    public int multiply(int a, int b) {
        return calculator.add(a, b);

We can write a Mockito test to mock the Calculator interface and test the CalculatorService:

import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;

public class CalculatorServiceTest {

    public void testMultiply() {
        Calculator calculatorMock = mock(Calculator.class);
        when(calculatorMock.add(2, 3)).thenReturn(5);

        CalculatorService calculatorService = new CalculatorService(calculatorMock);

        int result = calculatorService.multiply(2, 3);

        assertEquals(5, result);
        verify(calculatorMock, times(1)).add(2, 3);

2. PowerMock


  • PowerMock is an extension to Mockito (and other mocking frameworks) that provides additional features, such as mocking static and private methods, constructors, and final classes.
  • It is useful when dealing with legacy code or third-party libraries where refactoring is difficult.


Suppose we have a Utils class with a static method:

public class Utils {
    public static String formatString(String input) {
        return "Formatted: " + input;

We want to test a class StringProcessor that uses this static method:

public class StringProcessor {
    public String processString(String input) {
        return Utils.formatString(input);

Using PowerMock, we can mock the Utils class and test StringProcessor:

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import static org.junit.Assert.assertEquals;

public class StringProcessorTest {

    public void testProcessString() {
        when(Utils.formatString("test")).thenReturn("Mocked: test");

        StringProcessor stringProcessor = new StringProcessor();
        String result = stringProcessor.processString("test");

        assertEquals("Mocked: test", result);

3. WireMock


  • WireMock is a library for stubbing and mocking HTTP-based services.
  • It is used for testing interactions with external systems, such as RESTful APIs.
  • WireMock allows you to simulate HTTP responses from external APIs without actually calling them.


Suppose we have a UserService class that interacts with an external User API:

import org.springframework.web.client.RestTemplate;

public class UserService {
    private RestTemplate restTemplate;

    public UserService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;

    public String getUserDetails(String userId) {
        String url = "" + userId;
        return restTemplate.getForObject(url, String.class);

Using WireMock, we can create a stub for the external API and test UserService:

import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.web.client.RestTemplate;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class UserServiceTest {

    private static WireMockServer wireMockServer;

    public static void setUp() {
        wireMockServer = new WireMockServer();
        configureFor("localhost", wireMockServer.port());

    public static void tearDown() {

    public void testGetUserDetails() {
        String userId = "123";
        String apiUrl = "/users/" + userId;
        String responseBody = "User details for " + userId;


        RestTemplate restTemplate = new RestTemplate();
        UserService userService = new UserService(restTemplate);

        String result = userService.getUserDetails(userId);

        assertEquals(responseBody, result);


  • Mockito is used for creating and configuring mock objects of interfaces and classes.
  • PowerMock extends Mockito and allows mocking static and private methods, constructors, and final classes.
  • WireMock is used for stubbing and mocking HTTP-based services, particularly useful for testing interactions with external APIs.

Depending on your testing needs, you may use these frameworks individually or together to comprehensively test your Java applications. Each serves a specific purpose and provides valuable tools for writing effective unit and integration tests.

Leave a Reply