Java abstract static Workaround

There are lots of answers about 'this does'nt make sense..' but indeed I met a similar problem just yesterday.

I wanted to use inheritance with my unit tests. I have an API and several its implementations. So I need only 1 set of unit tests for all implementations but with different setUp methods which are static.

Workaround: all tests are abstract classes, with some static fields with protected access modifier. In all implementations I added static methods which set these static fields. It works rather nice, and I avoided copy and paste.


You have a couple of options:

  1. Use reflection to see if the method exists and then call it.
  2. Create an annotation for the static method named something like @GetAllWidgetsMethod.

  3. As others have said, try to not use a static method.