Scala objects? Are they evil?

20 views
Skip to first unread message

matroska

unread,
Oct 19, 2009, 4:58:46 PM10/19/09
to Bay Area Scala Enthusiasts
Hi all,

I was wondering which is the right approach to follow when you want to
test classes that uses singleton objects. Should we avoid the use of
singleton "object" in our scala programs? For example, if we think to
have DAO objects, they should be singleton right? But at the same time
we need to test the classes that use them in some way. The problem is
that with the syntax we use in scala we would call the singleton dao
with Dao.getAllStudents() within the classes that use the dao and I
personally cannot understand how to mock them. Should we avoid
objects? How could we test classes that use singleton?

Thanks in advance
Tobia Loschiavo

Dick Wall

unread,
Oct 19, 2009, 5:11:10 PM10/19/09
to scala...@googlegroups.com
Scala gives you all sorts of possibilities for testing Singletons, but
if you control the code-base the best bet is to avoid traditional Gang
of Four singletons entirely. I would recommend looking at dependency
injection as an alternative - something like Guice or the cake pattern
demoed by Martin Odersky (personally I use Guice because of
familiarity and also I find it slightly more powerful). Using DI will
let you have an easy to test codebase and still control everything
about the unit testing of the classes, and you can still effectively
have singletons (the @Singleton annotation in Guice) without paying
the cost of that hard-wiring.

Scala objects are useful for a bunch of other things too though, for
example companion objects, case objects, etc. so I don't believe a
general rule about avoiding them is a good idea.
--
Dick Wall
Email: di...@bldc.org Gmail: dick...@gmail.com Skype: dickwall
AIM: QuaintRcky
Co-Host of the Java Posse: http://javaposse.com Blog:
http://dickwallsblog.blogspot.com

Kevin Wright

unread,
Oct 19, 2009, 5:18:50 PM10/19/09
to scala...@googlegroups.com
singletons can inherit from other classes and from traits, so one
possibility is to write your tests against one of these.

It's also not right to just think of a singleton as global state,
classes can have inner singletons too - in which case you'll actually
end up with one singleton per instance of that class :)

Tobia Loschiavo

unread,
Oct 20, 2009, 8:39:27 AM10/20/09
to Bay Area Scala Enthusiasts
Thank you for your answers.
I have no experience on Guice but I think it should be simple. Anyway
I would like to use Guice without using @Singleton on classes but
keeping using @Inject onto scala objects. Is it possible? Is there any
example? I should also avoid setter injection (that would involve var
usage) and use constructor injection instead. Is this right?

Thank you again.
Tobia Loschiavo

On Oct 19, 11:18 pm, Kevin Wright <kev.lee.wri...@googlemail.com>
wrote:
> singletons can inherit from other classes and from traits, so one
> possibility is to write your tests against one of these.
>
> It's also not right to just think of a singleton as global state,
> classes can have inner singletons too - in which case you'll actually
> end up with one singleton per instance of that class :)
>
Reply all
Reply to author
Forward
0 new messages