I have a service class that performs some operations. One of the operations is a piece of code long enough to warrant extracting to a new class and unit test it in isolation:
@Service
public class ItemFinalizer {
private final ItemPublisher itemPublisher;
private final FollowupScanPublisher followupScanPublisher;
public void finalizeItem(Item item) {
itemPublisher.publish(item);
handleFollowUpScan(item);
}
private void handleFollowUpScan(Item item) {
ScanType followUpScanType = null;
if (item.getScanType() == ScanType.STANDARD) {
if (item.getBank().containsFlag(BankFlagEnum.AUTO_STANDARD_TO_ADVANCED)) {
followUpScanType = ScanType.ADVANCED;
} else if (item.getBank().containsFlag(BankFlagEnum.AUTO_STANDARD_TO_FULL_BOARDING)) {
followUpScanType = ScanType.FULL_ONBOARDING;
}
} else if (item.getScanType() == ScanType.SIGNUP &&
item.getBank().containsFlag(BankFlagEnum.SINGUP_BOARDING_COMPLETE_CYCLE)) {
followUpScanType = ScanType.FULL_BOARDING;
}
if (followUpScanType != null && item.isRegistered()) {
FollowUpScanRequest followUpScanRequest = new FollowUpScanRequest(item.getId(), followUpScanType.getId());
followupScanPublisher.publish(JsonUtils.toJson(followUpScanRequest));
}
}
}
After extracting the first 11 lines of the handleFollowupScan
method (pure business logic) to an external class, it looks like this:
private void handleFollowUpScan(Item item) {
ScanType followUpScanType = FollowupScanExtractor.extractFollowupScan(item);
if (followUpScanType != null && item.isRegistered()) {
FollowUpScanRequest followUpScanRequest = new FollowUpScanRequest(item.getId(), followUpScanType.getId());
followupScanPublisher.publish(JsonUtils.toJson(followUpScanRequest));
}
}
The question is - should the new method (FollowupScanExtractor::extractFollowupScan
) be static?
On one hand, there's no reason for it not to be static, as this is a pure function with a deterministic result (you'll always get the same output for the same input). It will also never be mocked, and can be very easily tested.
On the other hand, this isn't a "utility" class per se, as the code in there is purely business logic, hence it kind of violates the mainstream usage of static classes.
I considered extracting it to a regular (non-static) class or create a spring bean (@Component) for holding that logic (I'm running inside a spring boot container, if that matters).