I have a singleton class like below :
public enum SingletonClassA {
INSTANCE;
private final Map<Character, Character> characters;
// private constructor
SingletonClassA() {
Map<Character, Character> aCharMap = new HashMap();
aCharMap.put('a', 'e');
aCharMap.put('o', 'u');
// in order to keep short I erased other puts.
characters = aCharMap;
}
public boolean containsKey(char letter) {
return characters.containsKey(letter);
}
}
And in order to test that I've create only one single object, even though I call more than once, I created a test case with JUnit :
public class SingletonTest {
@Test
public void TestSingletonObject(){
SingletonClassA instance1 = SingletonClassA.INSTANCE;
SingletonClassA instance2 = SingletonClassA.INSTANCE;
//Passes
Assert.assertSame("2 objects are same", instance1, instance2);
}
@Test
public void TestgetInstance(){
SingletonClassA instance1 = SingletonClassA.INSTANCE;
SingletonClassA instance2 = SingletonClassA.INSTANCE;
// Does not pass
Assert.assertSame(instance1.getInstance('o'), instance2.getInstance('o'));
}
}
The test passes from TestSingletonObject() which says that those 2 objects are exactly the same. But from the second one, TestgetInstance(), it does not pass.
My question is: why? Why it does not pass from the second test. I am thinking that even I call the instance methods it should return true because they belong to the exact same object. Am I missing a point?
Aucun commentaire:
Enregistrer un commentaire