November 2018
Intermediate to advanced
388 pages
9h 5m
English
Now let's write a test class to verify the singleton class.
Consider the following test case:
@Test public void testSingletonObject() { Singleton singleton = Singleton.getInstance (); Singleton anotherSingleton = Singleton.getInstance (); System.out.println (singleton); System.out.println (anotherSingleton); System.out.println (singleton == anotherSingleton); Assert.assertEquals (singleton,anotherSingleton); }
In this case, we are trying to instantiate the singleton class twice. We print the two instance objects that are initialized. Since the same object is returned every time the getInstance() function is invoked, it prints the same memory address at which these instances are loaded. The comparison ...
Read now
Unlock full access