Reputation: 233
I have a static function that returns void.
This function performs some operation and populates a map internally.
Something like this...
public static void doSomething(arg1,arg2,arg3) {
//Do some processing with the input parameters
if(arg1 satisfies given condition) {
//Create object
Conn conn = new Conn()
Map<String, List<String>> map = new Map();
//Populate the map with the processed value
conn.setAttributes(map);
}
}
Now I want to test if the condition is satisfied whether my 'map' in 'conn' object is populated with the expected values or not? Need help on this.
Upvotes: 0
Views: 101
Reputation: 15622
The problem here is the hidden dependency, the instantiation of the Conn
object.
As a rule of thumb business code should not be responsible for aquiering dependencies it has to comunicate with.
You have two solutions:
Surrender to your bad design and use Powermock to inject a Mock of Conn
.
Improve your design and use dependency injection/inversion of control.
This can be done by simply replace the inner instantiation of the Conn
object with an additional Parameter of type Conn
.
But even better would be to turn your static
method into a regular non-static member method and pass the instance of Conn
as Constructor parameter to be held in a private final
member variable.
Given that class Conn
is not final
you can use regual mocking frmeworks to create mocks of Conn
and use that in your UnitTests.
Upvotes: 1