Mocking Final Classes with Mockito

I have used PowerMock with Mockito for mocking final Class.

Following are Maven dependencies for PowerMock and Mockito.
Eg. pom.xml



We have final class called with public method called Send(String parameter).
Need to mock Send method.

How we do that?

Eg: Final Class is as follows

final class TestFinal{

       public String send(String input){

       //method to process the input
       String status=Processor.getResult(input);

       return status;



Mocking send method

import java.util.HashMap;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

public class RequestFlowTest {

    public void shouldTestRequestData() throws Exception {
 	TestFinal testFinal = PowerMockito.mock(TestFinal.class);

 	String result=testFinal.send("testInput");


Tagged with: , , , , , ,
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: