I'd suggest (by the order of my preferences):
- Wrap this with a concret implementation and mock the concreted class
- Use a protected method with and extend the class with a testable class
- Use
PowerMock
Option 1 - Concrete implementation:
public class StaticClassWrapper() {
public Map method() {
return StaticClass.method();
}
}
Option 2 - Testable class
public class A {
public void testOne() {
Map map = method();
}
protected Map method() {
return StaticClass.method();
}
}
In your tests you need to create TestableA
and run the tests on it (instead of running tests on A
):
public class TestableA {
protected Map method() {
// return a mock here
}
}
Your last (and least favorite) option is to use PowerMock
. It can work with EasyMock
in order to mock static calls.