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?
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.
On Mon, Oct 19, 2009 at 1:58 PM, matroska <tobia.loschi...@gmail.com> wrote:
> 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?
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 :)
On Mon, Oct 19, 2009 at 9:58 PM, matroska <tobia.loschi...@gmail.com> wrote:
> 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?
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 :)
> On Mon, Oct 19, 2009 at 9:58 PM, matroska <tobia.loschi...@gmail.com> wrote:
> > 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?