Mocking Static Blocks in Java

后端 未结 10 1972
南方客
南方客 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:12

    I suppose you really want some kind of factory instead of the static initializer.

    Some mix of a singleton and an abstract factory would probably be able to get you the same functionality as today, and with good testability, but that would add quite a lot of boiler-plate code, so it might be better to just try to refactor the static stuff away completely or if you could at least get away with some less complex solution.

    Hard to tell if it´s possible without seeing your code though.

提交回复
热议问题