Video in progress

When is it fine to write a static helper method, and when should it be a bean with an instance method?

A function that is stateless, pure (the same input always gives the same output) and doesn't touch external resources can be static. If it depends on time, the DB, the network, configuration or randomness, it should be a bean, otherwise you can't replace it in a test.

📖 Answer

Good candidates for static:

static String maskCard(String pan) {
    return "**** " + pan.substring(pan.length() - 4);
}
static int percentOf(int value, int percent) { return value * percent / 100; }

They are deterministic and easy to test: input to output.

Things that should not be static:

static boolean isExpired(Subscription s) {
    return s.getEndsAt().isBefore(Instant.now());   // hidden dependency: the clock
}

static Rate currentRate() {
    return RestClient.create().get()...;             // network, timeouts, can't be mocked
}

To test the first one, you can't "freeze" time. The second sends a real HTTP request in every test. If you turn both into beans and pass Clock and RateClient through the constructor, you can replace them with fakes in tests.

Another sign: a chain of static calls to other static "services" inside a static method is hidden global state and a way around DI. You can mock it with Mockito's mockStatic, but that papers over the design problem instead of solving it.