Why is using static helper methods in Java bad?

后端 未结 5 1647
北恋
北恋 2020-12-15 05:19

I\'m asking because I\'m trying to use a mocking framework (Mockito) which does not allow you to mock static methods. Looking into it I\'ve found quite a few blog posts sayi

5条回答
  •  臣服心动
    2020-12-15 05:54

    It is less modular. Instead you should define an interface ApiCaller with an instance method makeHttpCall() so that you can define separate implementations in the future.

    In the very least you will always have 2 implementations of an interface, the original and the mocked version.

    (Note: there are some mocking frameworks that allow you to mock static methods)

    As an addendum, while this may not be the case in your specific application, typically the use of static methods is indicative of a larger design oversight. Designing for modularity and reuseability should be prevalent throughout your application, because even though you don't need it right now you may need it in the future, and it's much harder and much more time consuming to change things after the fact.

提交回复
热议问题