Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing with Spring framework singleton beans

It’s a general consensus that Singleton is bad for unit-testing.

But what about having an IoC container like the Spring framework to control your beans which are singletons by default? Is using those beans in your classes also considered bad for unit testing the same way singleton is?

like image 908
Glide Avatar asked Feb 26 '23 17:02

Glide


1 Answers

Singletons are not a bad thing. They are very much a good thing.

The Singleton Design Pattern, however, is generally considered bad, since it can hamper testing.

When you have an IoC framework like Spring to manage your singletons for you, you're fine.

Spring proves a unit testing framework which manages singletons during the test lifecycle.

like image 147
skaffman Avatar answered Mar 01 '23 09:03

skaffman