O'Reilly logo

Quality Code: Software Testing Principles, Practices, and Patterns by Stephen Vance

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Testing the Singleton Nature

Focusing on the singleton nature, there are two essential behaviors and two optional behaviors we need to test. The two essential behaviors are

• That getInstance() returns an instance of the class

• That getInstance() returns the same instance for all invocations

Let’s address these and then talk about the optional behaviors. Note that we did not say anything about the initialized state of the instance. That is not part of the value added by the getInstance() method. Rather, it is part of the value added by the class member initialization and the constructor, which are a separate bundle of intents.

The second behavior, that the singleton always returns the same instance, is the behavior that makes singletons difficult ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required