Different Singleton instances with JUnit tests

后端 未结 7 937
遥遥无期
遥遥无期 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:09

    generally beware of singletons, most often they are evil, bad design and tend to represent big yucky global variables (which is bad for maintenance).

    still to get tests in place first you can do:

    
    static setInstance(...){ //package visibility or in difficult cases you have to use public
      instance = ...;
    }
    
    

    as said this is more a workaround. so get first tests place, but then refactor away from singleton pattern.

提交回复
热议问题