Tuesday, April 5, 2011

Little Spring Gem: ReflectionTestUtils

I have recently been slacking on content on my blog, between long stressful hours at work and to the wonderful toy that is an iPhone, I have taken a little break from anything "development" related after leaving the office to maintain my sanity. Now with the project delivered, and things quieting down again I can re-focus my excess neuron energy back to processing more IT related information.

One little thing I discovered in my last project, which I am a little embarrassed about, being a Spring nut, is the little gem that is : ReflectionTestUtils.

I mostly try write unit tests that do not include Spring, for the reason, that you should be testing your code and not Spring config. However sometimes it's really useful and quite beneficial to have your test code wired up with Spring, be that for integration tests or just to extend your test suite.
One issue was, I always found myself adding "setters" to my component interfaces, I always hated doing it, but I would con myself by saying: "It was for more testing, and more testing is always a good thing", and move along. I eventually (while procrastinating on work I didn't really want to do) went searching for a cleaner solution.

1 search and 1 minute later, a little red faced, I kicked myself. I should have guessed straight away that Spring would have thought of this scenario From 2.5 Spring had the ReflectionTestUtils class, which simply lets you set your dependencies / mocks via reflection.

So easy, tell it which object, the name of the field and set the mock / value you want to set. Neater interfaces, good times.
Below is an example using EasyMock interfaces and inject them for my test.

34 comments:

  1. Hi Brian,

    It is kind of funny, because I spent some time in my last project with removing ReflectionTestUtils calls from our code :-)

    I believe that reflection makes the code hard to read, and when it comes to refactoring (even for test code) it is a nightmare.

    Additionally, I observed an interesting thing. After the ReflectionTestUtils appeared in the test cases, people started to use it, because it was easier, than taking care of dependency injection. So their first thought was to use the ReflectionTestUtils and not thinking about a way to inject classes properly.

    I'm not saying that ReflectionTestUtils is "evil", but watch out where and how are you using it.

    Cheers,
    Zsolt

    ReplyDelete
  2. Hi, Thanks for the comment.

    I agree, I am generally not a fan of reflection either and I always try avoid using it in code.

    I believe in this case it is handy, but as you mention unfortunately programmers often misuse things. I have seen people do unbelievably obscure things with many of the Spring features. Thankfully in my current environment most of those are picked up in code / peer reviews.

    Also in our case, using Maven and limiting the spring-test maven artifact to "test" scope should keep it out of general code.

    But...
    We all know us programmers are overly resourceful and reasonably lazy, so I'll heed your warning and will keep an eye out for it sneaking into where it doesn't belong.

    ReplyDelete
  3. ReflectionTestUtils is a gem in disguise. It can be very useful in our test code, no doubts. I too hate adding setters just because we need to set a mock object to an autowired member. But having said that, I learnt in Server side Java symposium (http://javasymposium.techtarget.com/html/agile.html#LKoskelaTestCode) that our test code should ideally be as good as production code.

    If we are mocking some members of class under test, and using the rest as autowired by Spring, why not mock'em all? Let's not be lazy. This approach will cause in longer tests(as per my experience), but these tests are much cleaner, and readable.

    ReplyDelete
  4. Why not add a test-only constructor that is package-protected and passes only fields you require in your tests?

    ReplyDelete
  5. It's nice that there are such blogs.

    ReplyDelete
  6. Thanks to this article I can learn more things.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hi,

    Where is the example test case?

    ReplyDelete
  9. Today there are number of spots all around the globe that can really be considered as best places to learn art and design.Image masking Service

    ReplyDelete
  10. he vast majority of these projects are offered on numerous grounds. Clipping Path Service

    ReplyDelete
  11. If we are mocking some members of the class under test and using the rest of the spring as autonomous power, why not mock everyone? Let's not be lazy. This approach will result in longer tests (in my experience), but these tests are more clear and readable.

    ReplyDelete
  12. pinoy channel give me a best platform to you now abs-cbn and gma serial free

    ReplyDelete
  13. Astonishing, astoundingly valuable post I appreciated it and wish to see more posts like that structure this blog, I adored this blog this perhaps helpful to me as I additionally have a Photoshop Clipping path service site where we give all kind of Photoshop services and to give tips on Photoshop a clipping path blog i made is a nice way to learn different tips on photography and illustrations designs.hope to get increasingly valuable post Form this blog��

    ReplyDelete
  14. Amazing, astoundingly important post I valued it and wish to see more posts like that structure this blog, I revered this blog this maybe supportive to me as I also have a Photoshop Clipping path service web site where we give all sort of Photoshop services. Want to get progressively important post Form this blog, Again Thanks a lot😊

    ReplyDelete
  15. Nice post.
    We are providing best digital photo editing service.
    Clipping Path
    Clipping Path Service

    ReplyDelete
  16. awesome post.we are providing best digital photo editing service,clipping path service. thanks a lot.

    ReplyDelete
  17. Thanks For sharing. Your techniques is really exceptional.

    ReplyDelete
  18. Thankyou for sharing this quick stuff.. We are going to share with all of you the latest seriale online on fs online which you will be watch online in dh.

    ReplyDelete
  19. i am browsing this website dailly , and get nice facts from here all the time .

    ReplyDelete
  20. Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. Clipping Path Service

    ReplyDelete
  21. It was not first article by this author as I always found him as a talented author. Harley Quinn Blue Blazer

    ReplyDelete
  22. Nice post. Thank you to provide us this useful information.
    Ryo Hazuki Jacket

    ReplyDelete
  23. Today I'm search a good information and helpful post. I'm appreciate your writing. Thanks for sharing your good knowledge with us.

    Clipping Expert Asia

    ReplyDelete
  24. I really like your,it so informative.

    ReplyDelete
  25. Great experience share here! I'm appreciate your experience. Keep it.
    clipping path service

    ReplyDelete
  26. Our the purpose is to share the reviews about the latest Jackets,Coats and Vests also share the related Movies,Gaming, Casual,Faux Leather and Leather materials available Beth Dutton Blue Coat

    ReplyDelete
  27. This is really amazing, you are very skilled blogger. Visit Ogen Infosystem for professional website designing and SEO Services.
    SEO Service in Delhi

    ReplyDelete
  28. I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job ! kids costume

    ReplyDelete
  29. So nice and important article.Thank you so much for it from ecommerce photo editing service provider
    So nice and important article.Thank you so much for it from ecommerce photo editing service provider

    ReplyDelete
  30. Nice Blog !
    Here We are Specialist in Manufacturing of Movies, Gaming, Casual, Faux Leather Jackets, Coats And Vests See Mr Robot Jacket

    ReplyDelete
  31. Wow! That's a great and informative article. Thank you very much for it from ecommerce photo editing service provider

    ReplyDelete

Popular Posts

Followers