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.
Subscribe to:
Post Comments (Atom)
Popular Posts
-
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...
-
I make no claim to be a "computer scientist" or a software "engineer", those titles alone can spark some debate, I regar...
-
I saw an article (well more of a rant) the other day, by Rob Williams Brain Drain in enterprise Dev . I have to say, I do agree with some o...
-
This series of posts will be about me getting to grips with JBoss Drools . The reasoning behind it is: SAP bought out my company's curre...
-
I recently finished 97 Things every programmer should know . Well to be completely honest I did skim over a couple of the 97, but all and al...

Hi Brian,
ReplyDeleteIt 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
Hi, Thanks for the comment.
ReplyDeleteI 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.
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.
ReplyDeleteIf 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.
Why not add a test-only constructor that is package-protected and passes only fields you require in your tests?
ReplyDeleteIt's nice that there are such blogs.
ReplyDeleteThanks to this article I can learn more things.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi,
ReplyDeleteWhere is the example test case?
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
ReplyDeletehe vast majority of these projects are offered on numerous grounds. Clipping Path Service
ReplyDeleteIf 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.
ReplyDeletepinoy channel give me a best platform to you now abs-cbn and gma serial free
ReplyDeleteAstonishing, 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��
ReplyDeleteAmazing, 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😊
ReplyDeleteNice post.
ReplyDeleteWe are providing best digital photo editing service.
Clipping Path
Clipping Path Service
awesome post.we are providing best digital photo editing service,clipping path service. thanks a lot.
ReplyDeleteThanks For sharing. Your techniques is really exceptional.
ReplyDeleteThankyou 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.
ReplyDeletei am browsing this website dailly , and get nice facts from here all the time .
ReplyDeleteWow, 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
ReplyDeleteHi, Very good article on Little Spring Gem: ReflectionTestUtils,
ReplyDeleteThanks for sharing, keep up the good work.
Photoshop Clipping Path | Web Images Optimization | Images Background Remove | Natural Drop Shadow | Raster to Vector Conversion | Contact Us
It was not first article by this author as I always found him as a talented author. Harley Quinn Blue Blazer
ReplyDeleteNice post. Thank you to provide us this useful information.
ReplyDeleteRyo Hazuki Jacket
Today I'm search a good information and helpful post. I'm appreciate your writing. Thanks for sharing your good knowledge with us.
ReplyDeleteClipping Expert Asia
I really like your,it so informative.
ReplyDeleteGreat experience share here! I'm appreciate your experience. Keep it.
ReplyDeleteclipping path service
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
ReplyDeleteHi I m Harry Thomas working with Cash App Help. We work towards making the customer experience of making payments through Cash App simple and easier. Contact us for any type of query.
ReplyDeleteCash App Help
Cash App Help
Cash App Support
Contact Cash App Helpline
Cash App Help
Cash App Help
Cash App Support
Contact Cash App Helpline
Cash App Help
Cash App Help
Cash App Support
Contact Cash App Helpline
Cash App Help
Cash App Help
Cash App Support
Contact Cash App Helpline
This is really amazing, you are very skilled blogger. Visit Ogen Infosystem for professional website designing and SEO Services.
ReplyDeleteSEO Service in Delhi
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
ReplyDeleteSo nice and important article.Thank you so much for it from ecommerce photo editing service provider
ReplyDeleteSo nice and important article.Thank you so much for it from ecommerce photo editing service provider
Nice Blog !
ReplyDeleteHere We are Specialist in Manufacturing of Movies, Gaming, Casual, Faux Leather Jackets, Coats And Vests See Mr Robot Jacket
Need Avenger infinity War Products tony stark hoodie
ReplyDeleteWow! That's a great and informative article. Thank you very much for it from ecommerce photo editing service provider
ReplyDeleteIt's an impressive and so informative article. Thank you very much for it from e-commerce photo editing service
ReplyDeleteTreat your looks with the cool and smart outfit Bombshell Harley Quinn Jackets with free shipping.
ReplyDeletethanks for this wonderful article it was nice and amazing. Chronicles Santa Claus Coat
ReplyDeleteThis is one of the best work i came across today, keep it up. Your information is really appreciable.
ReplyDelete1000 free youtube subscribers
SMM service
Greetings from Foto Masking. Happy to surfing this post
ReplyDeleteThe great website and information shared are also very appreciable. Qbook
ReplyDelete
ReplyDeleteMarvelous post. The articles are well written. Your blog is really outstanding. Thanks a lot for sharing such a wonderful post.
Clipping Path
Image Background Removal Service
Image Masking Service
Image Manipulation Service
Photo Retouching Service
Shadow Creation Service
Color Correction Service
Clipping Path Service
Nice Post!
ReplyDeleteQuickBooks 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.
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
ReplyDeleteMarvelous post!!! Your blog is really outstanding. Thanks a lot for sharing such a wonderful post.
Clipping Path | Image Background Removal Service | Image Masking Service | Image Manipulation Service | Photo Retouching Service | Shadow Creation Service | Color Correction Service | Clipping Path Service
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.
ReplyDeleteWow, 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
ReplyDeleteso nice blog i like it so much thanks to sharing this informative blog with us. Cheerleading Glee Cheerios Bomber Jacket
ReplyDelete