Two of the most famous mocking frameworks EasyMock and Mockito, don't offer out of the box support for mocking final and static methods. It is often said on forums that "you don't want that" or "your code is badly designed" etc. Well this might be true some of the time, but not all of the time. So let's suppose you do have a valid reason to want to mock final or static methods, PowerMock allows you to do it. Here's how (example with Mockito):

1. add the following dependencies to your pom.

 <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.4.12</version>
    <scope>test</scope>
</dependency>

2. In your Testclass, prepare the classes you want to mock with PowerMock.

@RunWith(PowerMockRunner.class)
@PrepareForTest({FinalClass.class, StaticClass.class})
public class TestClass {
    ..
}

3a. Mock static code and specify the behaviour.

PowerMockito.mockStatic(StaticClass.class);
PowerMockito.when(StaticClass.staticMethod()).thenReturn(someObjectOrMock);

or, with i.e. 3 parameters:

PowerMockito.mockStatic(StaticClass.class);
PowerMockito.when(StaticClass.class, "staticMethod", param1, param2, param3).thenReturn(someObjectOrMock);

3b. Mock final methods

PowerMockito.when(FinalClass.class, "finalMethod").thenReturn(someObjectOrMock);
shadow-left