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.

118 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
  32. It's an impressive and so informative article. Thank you very much for it from e-commerce photo editing service

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

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

    ReplyDelete
  35. This is one of the best work i came across today, keep it up. Your information is really appreciable.

    1000 free youtube subscribers
    SMM service

    ReplyDelete
  36. Greetings from Foto Masking. Happy to surfing this post

    ReplyDelete
  37. The great website and information shared are also very appreciable. Qbook

    ReplyDelete
  38. Nice Post!
    QuickBooks is a pioneer accounting application for small business that has become a prior choice of every organization.In case you have faced any technical issues in QuickBooks, call us at QuickBooks Customer Service Number 1-855-974-6537.

    ReplyDelete
  39. 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
  40. 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
  41. Wow, this is very interesting reading. I found a lot of things which I need. Great job on this content. I like it Cyberpunk 2077 V Samurai Bomber Jacket

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

    ReplyDelete
  43. great and nice content quality also much informative

    ReplyDelete
  44. Nice Blog !
    Our team at QuickBooks Customer Service goes above and beyond to meet your expectation amid the toughest challenges.

    ReplyDelete
  45. 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

  46. 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
  47. Amazing Blog Thanks For sharing ... i am glad to read it out ...
    The Suicide Squad Savant Jacket

    ReplyDelete
  48. 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
  49. Thanks for sharing IT related information. Great and nice writing quality also much informative!
    Rent In Gulshan

    ReplyDelete
  50. If you can comment well, you will certainly obtain a lot of visitors with a follow-up web link. Below's a listing of some excellent Classified submission sites List.

    ReplyDelete
  51. Dubai Fun Club for premium Independent Escorts in Dubai and entertainment services. You can easily find the best call girls in Dubai on our website

    ReplyDelete
  52. Clipping path Services Company must be essential to create lifelike photos. When you have perfect customer will like it and buy your products best clipping path service provider

    ReplyDelete
  53. 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
  54. 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
  55. Very interesting subject, appreciate it for posting.

    my web site :휴게텔

    ReplyDelete
  56. 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
  57. i love the teenage robot

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

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

    ReplyDelete
  59. 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
  60. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. Afc Richmond Jacket

    ReplyDelete
  61. Thanks for informative post. This post has helped me save many hours of browsing other similar posts just to find what I was looking for. I’m going to bookmark this post.
    DESKTOP-I238E5L

    ReplyDelete
  62. 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
  63. 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
  64. The strangest figure that meets us in the annals of Oriental thought is that of Confucius.

    Confucius, "The Analects of Confucius", i.1

    ReplyDelete
  65. 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
  66. The workout clothes store offers every tank top for men and bodybuilding clothing items imaginable.

    Bodybuilding Clothing

    ReplyDelete
  67. 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
  68. organic cosmetics, jewelry, artistic and artisanal accessories. Ethical manufacturing in Portugal and EU with organic materials, recycled and upcycling.

    sustainable fashion brands

    ReplyDelete
  69. So much thanks for the sharing. I was looking for that and this is very important for me. Thanks!

    You can also visit: Image Masking Service

    ReplyDelete
  70. 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
  71. 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
  72. 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
  73. The workout clothes store offers every tank top for men and bodybuilding clothing items imaginable.

    https://www.bestforminc.com

    ReplyDelete
  74. Hello, I'm happy to see some great articles on your site. Would you like to come to my site later? My site also has posts, comments and communities similar to yours. Please visit and take a look 토토사이트

    ReplyDelete
  75. custom dropper bottle boxesOur custom dropper bottle boxes are the perfect packaging solution for all your essential oils and fragrances they are made of high quality material.

    ReplyDelete
  76. As I am looking at your writing, 바카라게임사이트 I regret being unable to do outdoor activities due to Corona 19, and I miss my old daily life. If you also miss the daily life of those days, would you please visit my site once? My site is a site where I post about photos and daily life when I was free.

    ReplyDelete
  77. 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
  78. Thanks for informative post. This post has helped me save many hours of browsing other similar posts just to find what I was looking for. I’m going to bookmark this post.

    Thanks
    image masking service

    ReplyDelete
  79. That's a great article! The neatly organized content is good to see. Can I quote a blog and write it on my blog? My blog has a variety of communities including these articles. Would you like to visit me later? 토토사이트추천

    ReplyDelete
  80. I've been searching for hours on this topic and finally found your post. casinocommunity, I have read your post and I am very impressed. We prefer your opinion and will visit this site frequently to refer to your opinion. When would you like to visit my site?

    ReplyDelete
  81. Medical Home Care BD is one of the best Nursing Home Care In Bangladesh. We also provide oxygen cylinder rent and sell,Oxygen Concentrator rent and sell, Nurse service, Elderly care service, New Born Baby Care service and medical equipments.We are 24/7 hours active to protect your health life. Oxygen Cylinder price in bd Oxygen concentrator price in bd Nursing home care in dhaka How to Breastfeed

    ReplyDelete

Popular Posts

Followers