Back Forum Reply New

Different approach to Unit testing

Hiho,

I have a question concerning JUnit testing with Spring..

My setup at the moment allows me to test my DAO Objects over a Mock JNDI lookup. My Service Layer uses some Mock Objects to be not dependent from the DAO Objects...

The question i got in mind was the problem that all my Mock Objects need actually to be tested as well ;-D

So why not to use my DAO Objects on their own and to use my DAO objects in the Service Layer instead of the mock objects...i know that this goes away from Unit testing and much more to Integration testing....

Why is it a wrong idea to use real Objects instead of the Mock Objects? As i first test my DAO totally isolated from the rest, i have the option to use quot;testedquot; objects to test my Service Layer...

What do you guys think on this?


Originally Posted by uenluenaHiho,

I have a question concerning JUnit testing with Spring..

My setup at the moment allows me to test my DAO Objects over a Mock JNDI lookup. My Service Layer uses some Mock Objects to be not dependent from the DAO Objects...

The question i got in mind was the problem that all my Mock Objects need actually to be tested as well ;-D

So why not to use my DAO Objects on their own and to use my DAO objects in the Service Layer instead of the mock objects...i know that this goes away from Unit testing and much more to Integration testing....

Why is it a wrong idea to use real Objects instead of the Mock Objects? As i first test my DAO totally isolated from the rest, i have the option to use quot;testedquot; objects to test my Service Layer...

What do you guys think on this?

If you use Spring as for example ioc cont in your Junit tests you have integration tests.
These tests are environment specific (even if run in integeration testing env) and usually takes much more than typical unit testing.
Spring creators mandates using integration testing mainly for integration layers(ie DAO layer), if you are more interested take a look at
presentations/s...ng-with-spring

Now consider Unit testing  - even run in Junit, are environment agnostic.
These typically should test only single class, mocking all its collaborators.
You can for example test your Service(Manager) class and check if it behaves as expected but try to imagine case when inside your service objects you access multiple other services, dao, integration adapters.
If you do not mock these collaborators your tests will:
- takes much longer
- if your test fail you do not know exactly where the problem is - in your service class or in one of your collaborators.
- much harder to prepareRemember it is all about separation of responsibility.

In your case if you decide to start service layer tests after all lower level test pass (your dao) layers introduces some kind of order dependency among your tests.
¥
Back Forum Reply New