JUnit: testing helper class with only static methods

There are several solutions:

  1. You can add a public constructor and call it from a test. While it doesn’t make sense, it also doesn’t hurt (much).

  2. Create a dummy static instance (you can call the private constructor here). Ugly but you can give the field a name to communicate your intent (JUST_TO_SILENCE_COBERTURA is a good name).

  3. You can let your test extend the helper class. That will intrinsically call the default constructor but your helper class can’t be final anymore.

I suggest the last approach especially because the class can’t be final anymore. If a consumer of your code wants to add another helper method, they can now extend the existing class and receive one handle to get to all helper methods. This creates a coupling of the helper methods which communicates the intent (these belong together) – which is impossible if the helper class is final

If you want to prevent users to accidentally instantiate the helper class, make it abstract instead of using a hidden constructor.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)