DEV Community

Discussion on: When Do We Want to Use a Static Class?

Collapse
 
rossholloway94 profile image
Ross Holloway

Important to keep in mind that static classes can be difficult to test.

Collapse
 
benfinkel profile image
Benjamin Finkel

Great point Ross! It’s easy to get yourself into trouble although I would argue they’re not difficult to test if you’re careful about their implementation. Too often you end up (as I did in my example) having a hard coded dependency on that static in your method which prevents it from being mocked.

One solution is to pass your static into the methods that rely upon it as a parameter, making it mockable and solving a common unit testing trip-up.