Behind that there's that old discussion about the usage of static methods against the principals of object orientation. This is another long discussion. In my humble opinion, static methods are really useful in some scenarios, like utility classes. Also, just remember Singleton... Well I'd better stop here with this topic. Let's get back to the mocking issue.
So, Mockito doesn't cover the static methods, but that's the one used in your project. So, why not make use the PowerMock just for the static methods? Yes, Mockito and PowerMock can be used together. You don't need to look at two distinct frameworks, you will be combining them. So, basically, PowerMock provides a class called PowerMockito to handle that. For everything else, you just use Mockito.
And it's pretty easy! I wrote a really simple test to prove it. First, these are my dependencies on Maven:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5.4</version>
<scope>test</scope>
</dependency>
Note I have both frameworks, and the PowerMock API to Mockito.
Your JUnit class test will need two annotations:
@RunWith(PowerMockRunner.class)@PrepareForTest(YourClass.class)
You'll have to define on the setUp method the results from the static method with given arguments:
@Before public void setUp() {
app = new App();
PowerMockito.mockStatic(YourClass.class);
PowerMockito.when(YourClass.isBlank(null)).thenReturn(true);
PowerMockito.when(YourClass.isBlank("")).thenReturn(true);
PowerMockito.when(YourClass.isBlank("X")).thenReturn(false);
}
Finally, you write you test:
public void testDoSomething() {
assertEquals("X", app.doSomething("X"));
assertEquals("No arguments!", app.doSomething(null));
}