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

22 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.

yanmaneee dijo...

moncler
kd 11 shoes
golden goose
adidas tubular
jordan store
golden goose sneakers
balenciaga sneakers
air max 97
michael kors outlet store
kobe shoes

Alex Smith dijo...

Hard to ignore such an amazing article like this. You really amazed me with your writing talent. Thank for you shared again.
Norton setup - Get started with Norton by downloading the setup and installing it on the device. Enter the unique 25-character alphanumeric product key for activation. Check your subscription norton.com/setup | norton.com/setup | norton.com/setup

Alex Smith dijo...

Thank you so much for sharing such a superb information's with us. Your website is very cool. we are impressed by the details that you have on your site.we Bookmarked this website. keep it up and again thanks
Login or sign up at office setup and download Microsoft Office. Install and activate the setup on your device. Verify the Office product key office.com/setup | norton.com/nu16

Alex Smith dijo...

Hard to ignore such an amazing article like this. You really amazed me with your writing talent. Thank for you shared again.
Norton setup - Get started with Norton by downloading the setup and installing it on the device. Enter the unique 25-character alphanumeric product key for activation. Check your subscription norton.com/setup | norton.com/setup | norton.com/setup

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

Alex Smith dijo...

Thank you so much for sharing such superb information's with us. Download the Microsoft Office setup on your Windows or Mac computer. Make sure that the Office product key is already copied for the activation procedure. office.com/setup mcafee.com/activate|office.com/setup

Alex Smith dijo...

Activating McAfee is a quick and simple process. You just need to copy your 25 characters alphanumeric product key and paste it in the activation tab. Let’s check out the step-by-step process #mcafee #activate
mcafee.com/activate | mcafee.com/activate | mcafee.com/activate | mcafee.com/activate

mcafee.com/activate dijo...

I am very thankfull to you for sharing this fantastic article , I appereciate your work .

mcafee.com/activate , mcafee.com/activate , mcafee.com/activate

James Boond dijo...

It read your article every thing is clear , Thanks for sharing.

office.com/setup , mcafee.com/activate

Unknown dijo...

Norton is one of the top most antivirus for computers and gadgets, to download and setup Norton antivirus you need to visit Norton.com/setup or www.norton.com/setup. In this article we will guide you all steps which are necessary to activate Norton antivirus on windows.

Nora Miller dijo...


Visit www.mcafee.com/activate for download and activate your mcafee product, and if you have any issues you can directly call at mcafee toll free number.

mcafee activation key support
activation key for mcafee
mcafee activation
how to activate mcafee
mcafee product activation
how to activate mcafee
mcafee com setup activation
mcafee antivirus code
mcafee activation support
mcafee activation code
mcafee activation key


i-nortonnorton dijo...

If you face any trouble at www.norton.com/setup in the course of redeeming Product key, down load and installing norton setup, Please call us toll-free USA/CANADA: +1-855-619-5888
Australia: +61-800-941-031
UK: +44-800-041-8972

Mark William dijo...

Trend Micro Internet Security subscription is the single way to download the software on multiple devices through www.trendmicro.com/downloadme . Trend micro has become a superior antivirus program over many antiviruses due to its advanced protection features. trend micro download process becomes easy when you purchase the subscription.

Mark William dijo...

eset internet security can be installed quickly on each device if the user follows the correct procedure. You can follow the instructions below to install the setup on your devices such as Windows PC, Mac, or mobile device. Best IT security solutions for your home and business devices. Try ESET antivirus and internet security solutions for Windows, Android, Mac or Linux OS.

sigmamarry dijo...

McAfee Antivirus additionally squares malware that incorporates noxious sites, rootkits, Trojans, infections, and phishing plans. It gives you in-program reports about the dangers it squares. McAfee Antivirus records the IP tends to associated with the danger, which permits technically knowledgeable clients to maintain a strategic distance from sites associated with those addresses.
mcafee.com/activate | mcafee login | install mcafee with activation code

sigmamarry dijo...

mcafee.com/activate as antivirus of virus furnishes you with the security shields to prevent malicious documents from entering in the Desktop, PC/cell phone by means of malware, spyware, rootkit, and Trojan ponies. As individuals all around the globe complete their regular work on their official and PCs, they require absolute protection from a wide range of dangers that these infection records can cause. These infections are produced abruptly from old PC-stuck records or dubious Trojan pony, spyware, and malware online sites.
mcafee.com/activate | www.mcafee.com/activate | mcafee download | mcafee activate

sigmamarry dijo...

Hey its john,I joned this communities for purpose of promote my technical site.Norton setup is where do you enter the Norton Setup Key at norton.com/setup to enact and introduce Norton item. Norton password generatot tools avoilable on Norton website : Norton setup
norton.com/setup || norton download | norton login | norton.com/setup | Norton setup | norton password generator

sigmamarry dijo...

microsoft office setup is truly outstanding and the greatest of the profitability programming's accessible in the efficiency programming industry. The product business has grown a ton in the 21st century,official website is here for visiting: office.com/setup we have even observed a lot of programming that are presently incorporated with the product that can really give an extraordinary lift to the product business.