Different Singleton instances with JUnit tests

后端 未结 7 940
遥遥无期
遥遥无期 2021-01-31 17:41

I have a standalone singleton which successfully passes the test. But with a group of tests this fails since once a singleton is defined it does not allow to reset the instance.

7条回答
  •  独厮守ぢ
    2021-01-31 18:01

    You can add a method to destroy the singleton, for example destroyMe(); where you deinitialize everything and set the instance of the singleton to null.

      public void destroyMe(){
       this.instance = null;
       //-- other stuff to turn it off.
    }
    

    I will leave synchronization problems though ;)

    But why do you need to re-initialize your singleton for each test? It should not differ based on the concept of the singleton.

提交回复
热议问题