Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it hard to unit test a system that depends on singletons?

Tags:

I've read cases for and against using the singleton pattern. One common case against describes the difficulties in unit testing with singletons, but I'm unclear as to why this is? If the unit test is part of the build couldn't you just reference the singleton and use it when its needed? (I'm thinking from a java perspective but I guess it shouldnt matter)

like image 514
ewa Avatar asked Oct 06 '10 21:10

ewa


1 Answers

A great article about this is Singletons are Pathological Liars. This describes, using a simple example, why testing using singletons is unexpectedly hard.

like image 81
Greg Hewgill Avatar answered Sep 18 '22 18:09

Greg Hewgill