且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

静态方法和单元测试

更新时间:2023-11-16 22:27:34

静态方法很容易测试,但直接调用静态方法的东西通常不容易独立于它依赖的静态方法进行测试。使用非静态方法,您可以使用stub / mock / fake实例来简化测试,但是如果您测试的代码调用静态方法,它将有效地硬连线到该静态方法。

A static method is easy to test, but something that directly calls a static method generally is not easy to test independent of the static method it depends on. With a non-static method you can use a stub/mock/fake instance to ease testing, but if the code you're testing calls static methods it's effectively "hard-wired" to that static method.