This is Pedrowaty's Typepad Profile.
Join Typepad and start following Pedrowaty's activity
Join Now!
Already a member? Sign In
Pedrowaty
Recent Activity
I'm also talking about unit tests, so you can use spring unit test runner ( @RunWith(SpringJUnit4ClassRunner.class)) which will do all the DI stuff for you or use implementation class instead of interface for your tests, even you code to interface, the test is for implementation not interface (many implementation means many tests). so it is normal to have sth. like this ServiceImpl serviceUnderTheTest; @Before public void prepare service() { serviceUnderTheTest = new ServiceImpl() serviceUnderTheTest.setCollaboratorA(new A() or mock(A)) serviceUnderTheTest.setCollaboratorB(new B() or mock(B)) ... } Never ever put setter to interface, because it is obvious that different implementation may not use and preferable implement this setter eg. ServiceInMemoryImpl may not use Dao at all.
I would use @Inject (no setter are needed, but java5 is :/) or in test don't follow strict rules just use ServiceImpl service = new ServiceImpl(); ServiceDao dao = new ServiceDao(); For test code it is OK.
Pedrowaty added a favorite at Marco Tedone's blog
Oct 18, 2010
Sorry but Facebook connect didn't work :<, so I Twitter-around the problem ;) I get your point but still, if your object need so heavy object graph it is "a smell". Think about somebody new who will maintain this test, how he/she knows which collaborators/value objects are important and what kind of influence on the service behavior they have? Should I have to read this XML document to understand object graph, I would prefer to read 200 java lines :). Moreover if the service will change. Should the test fails? and if yes what kind of information will I have? Is there any changes which makes that test won't fail and why? How should I prepare new test?, just record the behavior will not test if I was wrong, because I will have wrong recording. But this is life and I understand your way and I'm also walking through such paths :) Pedro
Pedrowaty is now following The Typepad Team
Oct 18, 2010