jueves, julio 26, 2012

Answering with Mockito




Overwhelmed by industry, Searching for a modern day savior from another place, Inclined toward charity (The Answer - Bad Religion)

When you are writing unit tests, you must keep in mind to not have dependencies to external components. To avoid this we use mock frameworks which for me the easiest one to use is Mockito.

In this post we are going to see an "advanced" technique used in Mockito to return same argument instance on a mocked method using Answer interface.

Suppose we are writing unit tests for class which manages Person and Job classes and as operation it uses a DAO class for inserting the relationship class (M:N) between Person and Job called PersonJob.

For example class under test will look something like:


So in this case it seems obvious that you need to mock personJobDao.

Let's create the mock and record the interaction:


Yes as you can see you don't know what to return, because instance is created by class under test and in the test method you don't know which instance is created by createPersonJob method. To solve this problem, you need to use thenAnswer instead of thenReturn method:

Note that Answer interface requires you to implement answer method, which in our case simply returns the first argument (PersonJob instance) of personJobDao.create method.

Now we can write assertions in peace without worrying about returned instance.

Hope you have found this post useful.

We keep learning
Alex.

Download Code
Music: http://www.youtube.com/watch?v=S2a3q0nIsoM

15 comentarios:

Anónimo dijo...

I wonder where is the real issue... Isn't it because createPersonJob method does too much, i.e. it knows about how to create a PersonJob object? If you introduce a factory like this:
PersonJob personJob = personJobFactory.create(person, job);
return this.personJobDao.create(personJob);
then this code is very simple to test.

--
Regards
Tomek Kaczanowski
http://practicalunittesting.com

Unknown dijo...

First of all thank you very much for reading my blog, well I agree with you that the solution you provided will make code simple to test, you would only need to mock personJobFactory and personJobDao. You can decide to create a factory for all your classes that requires a M:N relationship, this is good, but you can also follow Persistence Manager pattern which is the responsible (as shown in this post) of managing M:N relationships without a factory.

Regards,
Alex.

Tomasz Nurkiewicz dijo...

If you are forced to use Answers in Mockito, at least wrap them in well named, static methods available somewhere. You must agree this is much more readable:

when(personJobDao.create(any(PersonJob.class))).thenAnswer(withFirstArgument());

Unknown dijo...

Hi Tomasz I totally agree with you, in face I usually use this approach or for example when using Converter interface of Lambdaj project I use the same approach you pointed here, but I try to give a very simple example so it is compact, reader will not require to jump over functions to know how they are implemented. But of course in production code I will do what you commented in your comment.

Thank you very much for reading my blog.

Alex.

avawilliams1122 dijo...

office.com/setup microsoft office has thought of an assortment of new invigorates in its applications, for instance, word, surpass desires, powerpoint, access, and others. these new updates features are open to all microsoft customers from home to business to class use. this generally commended
office.com/setup

office.com/setup

mcafee.com/activate

mcafee.com/activate

mcafee.com/activate

mcafee.com/activate

AlexaMartin dijo...

I almost spent an hour on your blog reading several of your posts. I got clear idea of certain of my doubts. I really appreciate the posts you have shared. Keep sharing more!

office.com/setup

Lillian J. Turner dijo...

Nice Blog. Thanks for sharing with us. Such amazing information.

MyBlogger Club

Guest Posting Site

Best Guest Blogging Site

Guest Blogger

Guest Blogging Site

Mark Watson dijo...

On your Mac, go to Applications > Utilities, then select the Keychain Access app. Select the Password menu located on the left side. Simply type the word “base station” on the search bar. Click the base station keychain. Go to the attributes option. Simply select the Show airport extreme default password Button.

james Smith dijo...
Este comentario ha sido eliminado por el autor.
james Smith dijo...

cannon printers installation step process To begin, download the correct model drivers for the printers. Before downloading and installing it, make sure that the operating system version is compatible and follow the ij start cannon steps.

EZWorks dijo...

Animated whiteboard films may be an excellent alternative if you want to tell your message in an engaging and entertaining way. Whiteboard animation videos stand out as a means of communicating your message since they are simple, yet friendly and powerful. The numerous advantages of whiteboard films demonstrate that this format may be an effective marketing tool for your company or business. Get in touch with the best audio visual experts at EZ to get amazing whiteboard videos made!

John Ruth dijo...

Wow...Nice Post. I'm Really Appreciate your written skills.

hp officejet pro 8600 driver | www.hp.com/go/wirelessprintingsolutions.brother.com/windows | 123.hp.com/laserjet | 123.hp.com/laserjet

stesyveau dijo...

replica bags replica gucci bags f3n72v6n34 replica bags hermes try this x9g86t9b88 7a replica bags wholesale replica goyard bags replica chanel bags ebay blog link g9i51l8z82 replica bags in pakistan

Hostdocket dijo...

QuickBooks is one of the best accounting software available in the market. Designed to cater both medium and small businesses, this software has changed the way accounting, payroll and bookkeeping is done. lost QuickBooks product and license number as it’s, packed with powerful features, today more and more companies using it. However, it is a software and that’s why often face technical errors. But all these errors can be easily handled as many articles available in the internet.

Hostdocket dijo...

Learn about QuickBooks error code h202
Usually when working on QuickBooks, one might land into some or the other QuickBooks error. The reason behind these errors can be the code and algorithm involved in this software. However, one of the best things about QuickBooks is the fact that all the errors can be easily rectified using some set