So far my suggestion to my colleagues is to move the body of the static block into a
private staticmethod and call it
staticInit. This method can then be called from within the static block. For unit testing another class that depends on this class could easily mock
staticInitwith JMockit to not do anything. Let's see this in example.
Will be changed to
So that we can do the following in a JUnit.
However this solution also comes with its own problems. You can't run
ClassWithStaticInitTeston the same JVM since you actually want the static block to run for
What would be your way of accomplishing this task? Or any better, non-JMockit based solutions that you think would work cleaner? Please follow the discussion at StackOverflow.com.