I have a class which has a well-defined responsibility – to “Enrich” an object with the information it needs. This information is gathered from a variety of sources (Services). Eg:
public class Enricher
{
private CounterpartyService counterPartyService;
private BookingEntityService bookingEntityService;
private ExchangeRateService exchangeRateService;
private BrokerService brokerService;
... 6 more services
public EnrichedTradeRequest enrichTrade(TradeRequest request)
{
EnrichedTradeRequest enrichedRequest = new EnrichedRequest(request);
// Enrich with counterparty info
enrichedRequest = enrichCounterParty(enrichedRequest)
// Enrich with booking entity info
enrichedRequest = enrichBookingEntity(enrichedRequest)
// Enrich with exchange rate info
...
// Enrich with broker info
...
// ....etc
return enrichedRequest;
}
private EnrichedTradeRequest enrichCounterparty(EnrichedRequest enrichedRequest)
{
// Get info from CounterpartyService
// ...
return enrichedRequest;
}
The logic for “how” to enrich a request is contained here. The class could be extended for different types of trade for example.
We enrich the trade in one step because we don’t want any partially-enriched objects floating around, which wouldn’t make a lot of sense.
The class is really difficult to unit test, because it has so many collaborators (up to 12 other services it calls upon). I would need to mock up 12 services, each with 3 or 4 different methods.
How do I reduce the number of collaborators in this case, and make this code testable?
The fundamental problem is that you have too many collaborators. If its difficult to test, then design can probably be improved.
One option is to create a facade service that manages the interaction of the collaborators. In your case, you might have a hierarchy of facades, since having only one will just move the need to have a lot of mocks into another area, which doesn’t solve the problem. If possible, try to group services that are more likely to be used together into facades. Or alternatively, if you always call the same X methods across services together, put that functionality in a single method somewhere. If you have a single facade which in turn calls 3-4 other facades, each test will only need 3-4 mocks which is more manageable.
The end result of this will be that your ‘enricher’, will only call one facade service, so testing will be easy. The trade off being the need to test your facades, which will be manageable.