Mocking Static Blocks in Java

后端 未结 10 1992
南方客
南方客 2021-01-30 11:00

My motto for Java is \"just because Java has static blocks, it doesn\'t mean that you should be using them.\" Jokes aside, there are a lot of tricks in Java that make testing a

10条回答
  •  长发绾君心
    2021-01-30 11:27

    Not really an answer, but just wondering - isn't there any way to "reverse" the call to Mockit.redefineMethods?
    If no such explicit method exists, shouldn't executing it again in the following fashion do the trick?

    Mockit.redefineMethods(ClassWithStaticInit.class, ClassWithStaticInit.class);
    

    If such a method exists, you could execute it in the class' @AfterClass method, and test ClassWithStaticInitTest with the "original" static initializer block, as if nothing has changed, from the same JVM.

    This is just a hunch though, so I may be missing something.

提交回复
热议问题