• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Is ServiceLocator usage bad for testability?

 
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was watching this "Google Clean Code Talks - Don't Look For Things!" by Misko Hevery

He explains why ServiceLocator usage isn't so great when it comes to testability.

Also, a lot of other things that seem convenient, easy or what I am used to which are also, not so great when it comes to testability:
https://www.youtube.com/watch?v=RlfLCWKxHJ0

My focus was on Design Patterns, but I side-tripped to this because someone said "I don't have time here to say why Singleton is bad.  If you don't know go watch this series of talks."  Yak-shaving in learning?

I don't know, the ServiceLoader / Service Locator stuff is on the OCPJP exam, so I am going to make sure I know it.

Even in there, he says, well it is better than Singletons, but still complicates testability/testing...

It was non-obvious enough to me that I wanted to mention it here.

The talk is old and predates modules, but I am pretty sure the JPMS doesn't address the testability drawbacks at all.

Cheers!
 
Saloon Keeper
Posts: 24804
172
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'm allergic to videos, but the reason I'd avoid ServiceLocator in testing is that in order to locate a service, the service (or at least a mockup) has to be available. Unit tests should ideally be simple, so the more complexity you need in a test environment, the worse it is.
 
Jesse Silverman
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah on both parts.

This other related video really brings home how bad global state and singletons are for testing:
https://www.youtube.com/watch?v=-FRm3VPhseI

He actually draws out a lot of things we hand-wave and say quick sentences about.

From the point of view of testing, it sure looks pretty bad.

The funny thing is, I see why people want them.  They are so convenient.

Unless you plan to test your software properly.
Then, not so much.
 
Jesse Silverman
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I referenced a video that said yes, but it is also discussed in this guide for testability:
http://misko.hevery.com/code-reviewers-guide/
 
reply
    Bookmark Topic Watch Topic
  • New Topic