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.

110 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. awesome post.we are providing best digital photo editing service,clipping path service. thanks a lot.

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

    ReplyDelete
  15. 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
  16. 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
  17. It was not first article by this author as I always found him as a talented author. Harley Quinn Blue Blazer

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

    ReplyDelete
  19. 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
  20. Great experience share here! I'm appreciate your experience. Keep it.
    clipping path service

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

    ReplyDelete
  22. 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
  23. 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
  24. Wow! That's a great and informative article. Thank you very much for it from ecommerce photo editing service provider

    ReplyDelete
  25. It's an impressive and so informative article. Thank you very much for it from e-commerce photo editing service

    ReplyDelete
  26. Treat your looks with the cool and smart outfit Bombshell Harley Quinn Jackets with free shipping.

    ReplyDelete
  27. thanks for this wonderful article it was nice and amazing. Chronicles Santa Claus Coat

    ReplyDelete
  28. Photographing reflections is a fashion you're able to develop because photo reflections shadow are mostly unnoticed by the average individual, so therefore you have a chance at being exceptional and revealing the world something only you have seen.

    ReplyDelete
  29. Lakshay consultant is known for giving best services to there clients with full of honesty and dignity . Lakshay consultancy deals in Equities, Currencies, Commodities, IPOs, Stock Broker in Delhi , Bonds/NCDs through NSE, BSE, MCXSX, USE. Depository services through NSDL and CDSL.

    ReplyDelete
  30. so nice blog i like it so much thanks to sharing this informative blog with us. Cheerleading Glee Cheerios Bomber Jacket

    ReplyDelete
  31. great and nice content quality also much informative

    ReplyDelete
  32. Congrats!! That is the most energizing thing :) The deck is San Marco brand in Viva, shading cloth. All things considered, I've heard that you can't get it in the States. Laaaame! Canadian and European as it were. Yet, Home Depot has a modest tile we utilized in our entrance that is 12×24 with a comparative shading and example.

    Jewelry photo editing Service is essential in the e-commerce market. The size, color. Design of your product's image are different marketplaces, and e-commerce websites. Like Walmart, Alibaba, eBay, Best Buy, Ozon.Ru, Shopping, Magento, Big Commerce, AliExpress, etc. Your sales promotion requires retouch, shadow, background removal, size, color, and design editing.

    ReplyDelete

  33. Wow, pretty cool sharing. I really choose your post.
    I’m working with a image post production platform for the editing work.
    Anyways very good sharing; please keep sharing more. I’ll definitely visit your blog again
    Photo Editing Service

    ReplyDelete
  34. Amazing Blog Thanks For sharing ... i am glad to read it out ...
    The Suicide Squad Savant Jacket

    ReplyDelete
  35. This is an Excellent post you have shared with us. I really like and appreciate your post.Thank you so much. Photoshop Clipping Path Sample Work | Car Image Editing

    ReplyDelete
  36. Thanks for sharing IT related information. Great and nice writing quality also much informative!
    Rent In Gulshan

    ReplyDelete
  37. I got too much interesting stuff on your blog. I guess I am not the only one having all the enjoyment here! Keep up the good work F9 Jackets

    ReplyDelete
  38. Get a full understanding of What is others in storage Android. Also, know the tips to delete the files and data that you don’t need.

    ReplyDelete
  39. Very interesting subject, appreciate it for posting.

    my web site :휴게텔

    ReplyDelete
  40. Often it's really helpful and totally beneficial to have the test code wired up with Spring, either for integration tests or just to extend your test suite.
    Buy Verified Neteller Account

    ReplyDelete
  41. i love the teenage robot

    please dont let turner or disney take it over
    pme legend schoenene

    ReplyDelete
  42. The post is written in very a good manner and it contains many useful information for me. Visit my Website 카지노사이트
    (mm)

    ReplyDelete
  43. Photography is my potion, from my childhood life i am doing work on this sector. Love to write blog on photography tips and tutorial also publishing in various platform. Last 5 years, I am working dedicatedly in this industry. Here I would like to share my Professional knowledge which has proper guideline to enhance newbie photography career. Check out my latest pages
    Ecommerce Photo Editing Service
    Product Photo Editing Service
    Graphics Design Service
    Web Development Service

    ReplyDelete
  44. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Afc Richmond Jacket

    ReplyDelete
  45. image masking service is an important part of web design. It enables web designers to create the appearance of depth and relief in the background of their digital images. The use of this technique makes your site stand out in front of a busy, cluttered market. However, not all image masking services are created equally. Some are better than others and some provide more features. This article will help you understand what image masking service is, how it works, and which ones are worth your time and money.

    ReplyDelete
  46. Very cool Ty – congratulations! And what a lucky little guy to have such a beautiful piece of art to announce him to the world. I’m sure he’ll treasure that piece of his baby book. fjallraven raven 20

    ReplyDelete
  47. Set in an enchanted 32 acre landscaped park with lighted brick pathways along the shore of a beautiful lake where nature abounds. We invite you to experience the ultimate outdoor fun under a canopy of majestic oak trees.

    boggy Creek Airboats

    ReplyDelete
  48. Set in an enchanted 32 acre landscaped park with lighted brick pathways along the shore of a beautiful lake where nature abounds.

    airboat rides in orlando

    ReplyDelete
  49. organic cosmetics, jewelry, artistic and artisanal accessories. Ethical manufacturing in Portugal and EU with organic materials, recycled and upcycling.

    sustainable fashion brands

    ReplyDelete
  50. Set in an enchanted 32 acre landscaped park with lighted brick pathways along the shore of a beautiful lake where nature abounds.

    best airboat rides in orlando

    ReplyDelete
  51. IT industry is growing at a high speed with the technological advancement I have created my own website and some have created different websites like hulu in uk and others etc that kelp people to gain knowledge and share knowledge.

    ReplyDelete
  52. Set in an enchanted 32 acre landscaped park with lighted brick pathways along the shore of a beautiful lake where nature abounds.

    https://bcairboats.com

    ReplyDelete
  53. We have enjoyed visiting your website.The information that is on your website is very beneficial for us. india news agency.If you want to read latest news in Hindi then you can visit our website.You will feel very good coming here and you will get all the information in Hindi and in simple language.

    ReplyDelete
  54. That's a nice information you have shared about reflection based utility methods in your blog. Really liked the facts you have discussed above.

    ReplyDelete
  55. Very Important tips: Please visit my website. Cut Out Image

    ReplyDelete
  56. Can you write some more about your recent project. I am learning ruby and rails language. And wanted to gather more information like this. wish me luck.

    ReplyDelete
  57. I have shared some tips from my 6 years of experience to improve your career.
    You Can Also Visit:

    Edit Bg

    ReplyDelete
  58. By conveying a sense of identity through the transfer of that feeling to the minds of others by expressing the traditional natural nature through the shelter of colors, forms, words or metaphors revealed by the artist's own experience, art is called.
    You Can Also Visit:

    Edit Bg

    ReplyDelete
  59. I like this article.ladies bomber jacketThank for sharing this post.

    ReplyDelete
  60. This was truly the very thing I was looking for. Great article. Thank you so much for your valuable information from
    photo-editing

    ReplyDelete
  61. I really found your post helpful. Check out this related piece of post I wrote about “ differents types of power & hand tools ” that might also provide important information.

    ReplyDelete
  62. I really enjoyed reading this article about Zen in the art of IT. I think it is a great way to look at the technological world in a different light and appreciate how technology can be used to achieve inner peace. It reminded me of the philosophy behind minimalism, where we should strive towards simplicity and forming meaningful connections with technology rather than getting overwhelmed by its complexity. I think this article is especially relevant right now as our lives are increasingly intertwined with technology and understanding how to use it effectively can help us lead more fulfilling lives. Thanks for sharing!

    diy photo booth frame

    ReplyDelete
  63. This is a great article! I'm so impressed that you found the ReflectionTestUtils class. It really is a great little gem in the Spring toolkit. It makes testing so much easier and cleaner. I'm sure anyone who has ever done any Spring development has run into the same issue of having to add "setters" to component interfaces. But, thanks to ReflectionTestUtils, it's no longer necessary!
    Do you wanna know,If you can wear jewelry in passport photo

    ReplyDelete
  64. really ! its awesome & very informative and useful It related post.thans for it
    Fully Furnished apartment for short/long term rent at gulshan

    ReplyDelete
  65. Really amazing article. It’s really helpful. Thanks for sharing.

    ReplyDelete
  66. Information technology is the backbone of our digital world! It fuels innovation, transforms industries, and connects people globally. From smartphones to supercomputers, IT shapes our daily lives. Let's celebrate the tech wizards who keep us wired and connected Are you having difficulty completing academic projects, essays, or writing intriguing articles? Our skilled staff is dedicated to provide you with top-tier writing services that meet and exceed your expectations.editing help cheap academic essay writing

    ReplyDelete
  67. Revolutionize your E-commerce visuals with our Product Photo Editing and Retouching Services. We meticulously enhance product images, ensuring impeccable quality that captivates customers. Elevate your brand's online presence with our expertise in crafting stunning and compelling product visuals. Read more,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,!

    ReplyDelete
  68. Revolutionize your E-commerce visuals with our Product Photo Editing and Retouching Services. We meticulously enhance product images, ensuring impeccable quality that captivates customers. Elevate your brand's online presence with our expertise in crafting stunning and compelling product visuals. Read more,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,!

    ReplyDelete
  69. Revolutionize your E-commerce visuals with our Product Photo Editing and Retouching Services. We meticulously enhance product images, ensuring impeccable quality that captivates customers

    ReplyDelete

Popular Posts

Followers