How to create an instance of Singleton several times?

I need a singleton in my code. I implemented it in Java and it works well. The reason I did this is to make sure that there is only one instance of this class in the mulitple environment.

But now I want to test my Singleton object locally with Unit test. For this reason, I need to simulate another instance of this Singleton (an object that will be from another device). So, is it possible to create a Singleton instance a second time for testing purposes, or do I need to mock it?

I'm not sure, but I think it is possible using a different class loader?

+7
java singleton
source share
9 answers

You can invoke the private constructor of your singleton class using reflection to create a new instance of the class.

class MySingleton { private MySingleton() { } } class Test { public void test() throws Exception { Constructor<MySingleton> constructor = MySingleton.class.getConstructor(); constructor.setAccessible(true); MySingleton otherSingleton = constructor.newInstance(); } } 
+9
source share

Traditionally, Singleton creates its own instance, and it creates it only once. In this case, it is not possible to create a second instance.

If you use Dependency Injection, you can let the framework create a singleton for you. Singleton does not protect against other instances (i.e., has an open constructor), but the dependency injection infrastructure only creates an instance of one instance. In this case, you can create more instances for testing, and your object will not be cluttered with a single code.

+17
source share

The point of Singleton is that you can only instantiate once.

+16
source share

Syntax, by definition, can only be created once. However, the fact that your unit test requires two singletones is a strong sign that your object should not be anything else, and you should review the design of the singleton.

+6
source share

I am forced to publish this series of articles on how singletones destroy testability and poor design choices:

Short Description: Combining logic for what a class does with how it is created, makes code that is ugly for testing, and should be avoided.

+2
source share

First, why do you need to create a new singleton to run unit test? A unit test should not run simultaneously with a regular application, so you should have access to the original singleton without fear of changing it.

Is there a specific reason you need an explicit second singleton?

+2
source share

you can just create another static getInstance2 method that looks like this:

 class MySingleton { private MySingleton(){} private static MySingleton instance1 = new MySingleton(); private static MySingleton instance2 = new MySingleton(); public static MySingleton getInstance(){ return instance1; } public static MySingleton getInstance2(){ return instance2; } } 
+1
source share

Singleton ston=Singleton.getInstance(); will return a singleton object. Using the ston object, if we call the createNewSingleTonInstance() method, which is written in the Singleton class, you will get a new instance.

 public class Singleton { private String userName; private String Password; private static Singleton firstInstance=null; private Singleton(){} public static synchronized Singleton getInstance(){ if(firstInstance==null){ firstInstance=new Singleton(); firstInstance.setUserName("Prathap"); firstInstance.setPassword("Mandya"); } return firstInstance; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; } public void setPassword(String password) { Password = password; } public String getPassword() { return Password; } public Singleton createNewSingleTonInstance(){ Singleton s=new Singleton(); s.setUserName("ASDF"); s.setPassword("QWER"); return s; } } 
0
source share

You can save the key on the map and fill the instance with the key

 public class MultiSingleton { /**** Non-static Global Variables ***/ String status = ""; private BaseSmartCard bsc; /***********************************/ private static Object lockObject = new Object(); private String serialNo; private static Map<String, MultiSingleton> mappedObjects = new TreeMap<String, MultiSingleton>(); protected MultiSingleton() { } public static MultiSingleton getInstance(String serialNo,long slotNo){ if (mappedObjects.isEmpty() || !mappedObjects.containsKey(serialNo)) { MultiSingleton instance = new MultiSingleton(); instance.setSerialNo(serialNo); mappedObjects.put(serialNo, instance); return instance; } else if (mappedObjects.containsKey(serialNo)) { return mappedObjects.get(serialNo); }else { return null; } } 
0
source share

All Articles