The Concept of Mocking
To someone who is new to unit testing, the idea of mock objects can be confusing to say the least. I have covered in previous tutorials how to use various mock object frameworks (EasyMock and jmockit). However in this tutorial, we will focus on the concept of mocking in general. What is a mock object? What is it used for? Why can't I mock object XYZ? Let's look into these questions and maybe clear a bit of the air on the use of mock objects.

Not all code is self contained

When we learn to program, our objects are usually self contained. Any hello world has no dependencies on outside classes (System.out aside) and neither do many of the other classes we write in the process of learning a language. However, in the real world, software has dependencies. We have action classes that depend on services and services that depend on data access objects (DAOs) and the list goes on.

The idea of unit testing is that we want to test our code without testing the dependencies. This test allows you to verify that the code being tested works, regardless of it's dependencies. The theory is that if the code I write works as designed and my dependencies work as designed, then they should work together as designed. The code below would be an example of this:

import java.util.ArrayList;

public class Counter {
     public Counter() {

     public int count(ArrayList items) {
          int results = 0;

          for(Object curItem : items) {
               results ++;

          return results;

I know the above example is about as simple as you get, but it illustrates the point. If you wanted to test the method count, you would write at test that addressed how the count method works. You aren't trying to test that ArrayList works because you assume that it has been tested and works as designed. Your only goal is to test your use of ArrayList.

Now let's look at a slightly more realistic example. Let's consider the code below:

public class MichaelsAction extends ActionSupport {

     private LookupService service;

     private String key;

     public void setKey(String curKey) {
          key = curKey;

     public String getKey() {
          return key;

     public void setService(LookupService curService) {
          service = curService;

     public String doLookup() {
          if(StringUtils.isBlank(key)) {
               return FAILURE;

          List results = service.lookupByKey(key);

          if(results.size() > 0) {
               return SUCCESS;

          return FAILURE;

If we wanted to test the doLookup method in the above code, we would want to be able to test it without testing the lookupByKey method. For the sake of this test, we assume that the lookupByKey method is tested to work as designed. As long as I pass in the correct key, I will get back the correct results. In reality, we make sure that lookupByKey is also tested if it is code we wrote. We now have a dilemma. How do we test doLookup without executing lookupByKey?

Enter Mock Objects

The concept behind mock objects is that we want to create an object that will take the place of the real object. This mock object will expect a certain method to be called with certain parameters and when that happens, it will return an expected result. Using the above code as an example, let's say that when I pass in 1234 for my key to the service.lookupByKey call, I should get back a List with 4 values in it. Our mock object should expect lookupByKey to be called with the parameter "1234" and when that occurs, it will return a List with four objects in it.

How Mock Objects Work

There are many different mocking frameworks in the Java space. I am not going to discuss any of them in great detail here. However, I will discuss how they work and design considerations you need to take in mind because of their implementations.

There are essentially two main types of mock object frameworks, ones that are implemented via proxy and ones that are implemented via class remapping. Let's take a look at the first (and by far more popular) option, proxy.

A proxy object is an object that is used to take the place of a real object. In the case of mock objects, a proxy object is used to imitate the real object your code is dependent on. You create a proxy object with the mocking framework, and then set it on the object using either a setter or constructor. This points out an inherent issue with mocking using proxy objects. You have to be able to set the dependency up thru an external means. In other words, you can't create the dependency by calling new MyObject() since there is no way to mock that with a proxy object. This is one of the reasons Dependency Injection frameworks like Spring have taken off. They allow you to inject your proxy objects without modifying any code.

The second form of mocking is to remap the class file in the class loader. The mocking framework jmockit is the only framework I am aware of that currently exploits this ability for mock objects. The concept is relatively new (since JDK 1.5 although jmockit supports jdk1.4 thru other means as well) and is provided by the new java.lang.Insturment class. What happens is that you tell the class loader to remap the reference to the class file it will load. So let's say that I have a class MyDependency with the corresponding .class file called MyDependency.class and I want to mock it to use MyMock instead. By using this type of mock objects, you will actually remap in the classloader the reference from MyDependency to MyMock.class. This allows you to be able to mock objects that are created by using thew new operator. Although this approach provides more power than the proxy object approach, it is also harder/more confusing to get going given the knowledge of classloaders you need to really be able to use all its features.


Mock objects are a very valuable tool in testing. They provide you with the ability to test what you write without having to address dependency concerns.

83 Responses

  • prakash responded:

    short and sweet

  • Leo Arias responded:

    Shouldn't it be java.lang.Instrument? It says Insturment.

  • Tomek responded:


    nice introduction, but I guess you've never heard of jeasytest, which is neither proxy- nor instrument-based and can do magic as well. :) It uses AspectJ.
    BTW, there are probably more aspect-based solutions (jeasytest, amock) than instrumentation-based (jmockit only AFAIK), so part of your post in which you divide mocking framework into "proxy" and "instrumentation" is far from truth.

    There are also other ways of dealing with such code. I've devoted a whole serie of posts to the problem of testing code which uses "new" or static code calls - see here

    Tomek Kaczanowski

  • Krishna responded:

    Excellent explanation in simple terms !!

  • GeorgeP responded:

    Nice and quick introduction to the problematics. Thanks!

  • J-C responded:

    I really enjoyed it, congratulations!

  • chirag responded:

    Good artical about mocking i started working recently.
    I need help with mocking
    If you are free then Can you please contact me by e-mail



  • B responded:

    Your blog seems very nice.
    Since you encouraged lot to ask questions, here I am. :)
    I am new to unit testing tools.
    Question: Is it possible to test a servlet using PowerMock? If yes, would you be kind enough to provide a tutorial for the same?

    Thanks a lot for your time and help. (in advance)

  • Asif responded:

    Nice. Nice.. Nice...

  • Mayur Shukla responded:

    Short and simple. Very nice explanation for beginer like me. Thanks

  • sam responded:

    It is good tutorial for the beginners... got good understanding of mocking after reading this.

  • Raghavan alias Saravanan M responded:

    I came here from/through your book 'Pro Spring Batch'.

    I really like your website template. The font is very cool. I think it is Lucida Grande which is the default font of Mac OS.

    I am aware of the Mocking concept, still wanted to read your version :) I was not cheated :P It is very precise and excellent. Infact I wish I could have a little more and detailed explanation of the last section (mocking types - proxy and instrumentation). A code base would also add values. It is my personal preference!

    Raghavan alias Saravanan M
    Bengaluru | India

  • Basil Zocca responded:

    Nice One, Good Stuff


  • Abhay responded:

    Short n simple. nice tutorial for beginners like me :)
    Thanks & Regards,

  • superigno responded:

    Thank you very much.

  • Roghini responded:

    Excellent Explanation

  • vinod responded:

    short and meaningful. But it needs some more explanation, how do we mock it? static methods, private methods....

  • QT_f responded:

    Hi,I'm new to Unit Testing,
    I was recently introduced to the concepts of Powermock,easymock and jmock.
    I'm yet to realize why we chose one over the other, especially easymock and power mock. Can i have any help w.r.t this.
    I want to know the limitations of each and can both (easymock and power mock) be used together to have better test cases.

    Thanks in advance

  • replica rolex watches for sale responded:

    comique Pandaranol subitement entre Zane

  • swiss rolex replica watches responded:

    Salutations, mon pote! Je suis Barney d'Australie et je voudrais juste dire que votre blog est génial. Tout est si instructif, des vidéos aux articles à même les images.

  • uggs on sale responded:

    Wonderful information and straight towards the stage. I'm not positive if this is truly the best location to ask but would you men and women have any ideas on where to hire some qualified writers? Thanks

  • Replica Rolex Watches for sale responded:

    With its higher good quality and guarantee, far more and much more persons will pick out the duplicate Breitling observe.

  • Replica Rolex Watches for sale responded:

    The special characteristics of the certain view are highlighted applying the 8 screws that go in the bezel in direction of the back again.

  • cheap beats by dre for sale responded:

    This is finding somewhat a lot more subjective, but I a lot choose the Zune Marketplace. The interface is colorful, has a lot more flair, and some cool features like ??Mixview' that allow you quickly see connected albums, songs, or other users connected to what you are listening to. Clicking on 1 of those will center on that item, and another established of neighbors?¡À will arrive into watch, allowing you to navigate about exploring by comparable artists, songs, or users. Talking of users, the Zune Social?¡À is also fantastic fun, letting you discover others with shared tastes and getting to be friends with them. You then can listen to a playlist created primarily based on an amalgamation of what all your friends are listening to, that's also pleasant. Those concerned with privacy is going to be relieved to know it is possible to prevent the public from viewing your private listening habits in the event you so pick.

  • fake tag heuer online responded:

    You can find broad ranges of collections of watches throughout the globe.

  • patek phillipe replica 26058 responded:

    All watches go by way of quality control by our personal team of specialists ahead of cargo.

  • responded:

    Those concerned with privacy is going to be relieved to know it is possible to prevent the public from viewing your private listening habits in the event you so pick.

  • Water heater atwood responded:

    Most web customers make use of online to find solutions, items and information they need by using google like Yahoo, MSN and Google. Users mostly click on sites that choose to appear on the first web page of look for motor engine outcome. For average web customers, high position is related to credibility. In other terms, web customers believe that greater a particular site crops up in look for motor engine outcome, greater are its chances of being relevant to their look for.

  • Bhushan responded:

    Very nice explaination.. Easy to understand...

  • lucky patcher responded:

    could well be truly interested in ability to hear just what your thinking tend to be along with request you to submit the reply to the above mentined. I personally consider this particular will be a very useful argument.

  • tiles melbourne responded:

    very good pst, it is so useful

  • Escorts in Jaipur responded:

    its great post for people, i just amazed to see this great article. thanks for share with us.

  • Escorts in Bangalore responded:

    I just amazed to see this great post, I just want to read more your article.

  • obat polip hidung responded:

  • obat pengapuran tulang pinggang responded:

  • triflex capsule responded:

  • qnc jelly gamat responded:

  • Independent Escorts Services in Gurgaon responded:

    Your site is good Actually, i have seen your post and That was very informative and very entertaining for me. Thanks for posting Really Such Things. I should recommend your site to my friends.

  • Gurgaon Escorts Book Online responded:

    You’ve written nice post, I am gonna bookmark this page, thanks for info. I actually appreciate your own position and I will be sure to come back here.

  • obat herbal penyakit hidronefrosis responded:

    I can feel that the articles contained in this blog is so interesting. I also get a variety informasin, thanks.

  • Gurgaon Call Girls responded:

    Well composed articles like yours renews my faith in today’s writers. You’ve written information I can ultimately agree with as well as make use of.

  • baidu root responded:

    Simply respecting your work and considering how you dealt with this web journal so well. It's remarkable to the point that I can't stand to not experience this important data at whatever point I surf the web!

  • Questions to Ask responded:

    I think this is a better than average article. You make this data intriguing and locks in. You give perusers a ton to consider and I welcome that sort of composing.

  • Hero Instinct responded:

    Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic.

  • Hero Instinct responded:

    A debt of gratitude is in order for the blog entry mate! Keep them coming...

  • Female Gurgaon Escort Services responded:

    Thanks for sharing so signification article with us. I agree with your idea completely.I am looking forward to another great article from you.

  • Stroke by Stroke responded:

    I gotta most loved this site it appears to be exceptionally useful .

  • tour operators in delhi responded:

    imperial voyages travel and tour operators in delhi we provide all affordable travel and tour packages at lowest price, if you need tour packages in india please visit our website.

  • Buy Cheap Essay-Dissertation Mill responded:

    I got it to my pertinent data from here. Much obliged to you such a great amount for sharing this post.

  • Coursework Writing Help responded:

    This is a champion among the most enlightening and educational posts. Really, I'm checking for this sort of material. Much gratefulness to you such a stunning aggregate for sharing this post.

  • Please Write My Assignment responded:

    This is a staggeringly illuminating article. I in, unmistakably, concur with your post title and your truly well portray your perspective. I am to a shocking degree bewildering to see this post. A commitment of gratefulness is all together for an offer with us. Keep it up and share the all the more most related post.

  • Please Write My Essay responded:

    The idea driving fake items is that we need to make a protest that will replace the genuine question. This taunt protest will anticipate that a specific strategy will be called with specific parameters and when that happens, it will restore a normal outcome. Utilizing the above code for instance, suppose that when I go in 1234 for my key to the service.lookupByKey call, I ought to get back a List with 4 esteems in it. Our taunt question ought to anticipate that lookupByKey will be called with the parameter "1234" and when that happens, it will restore a List with four protests in it.

  • EssayAvenue responded:

    This is a staggeringly enlightening article. I in, unmistakably, agree with your post title and your really well depict your point of view. I am to a stunning degree puzzling to see this post. A dedication of appreciation is all together for an offer with us. Keep it up and share the more most related post.

  • Love quotes responded:

    You know your activities emerge of the group. There is something exceptional about them. It appears to me every one of them are truly splendid!

  • free walking tour stockholm responded:

    If I had to use one word to describe this blog it would be GRANDILOQUENT.

  • Vikas Kashyap SEO in Delhi responded:

    I would like to say thank you for the amazing details and concepts you are sharing in this.

  • Cheap Postcard Save Dates responded:

    The entire process of sending out cheap postcard save the dates is easy and economical

  • responded:

    I appreciate what you have done here. I like the part where you say you are doing this to give back yet I would expect by every one of the remarks this is working for you too.

  • 100 Questions responded:

    Awesome! It sounds great. A debt of gratitude is in order for sharing..

  • Cara mengatasi ginjal bocor responded:

    thank you very useful information admin, and pardon me permission to share articles here may help

  • hyundai elantra car cover responded:

    Awesome is the most appropriate word to portray this blog.

  • Independent Female Gurgaon Escorts responded:

    Really structured and useful information. And everything is clear, thanks. I looked through some posts and must say, they are very interesting.

  • Make Him Desire You responded:

    A debt of gratitude is in order for your data, it was truly exceptionally helpfull..

  • voyance pas cher par telephone responded:

    Of course, some high or advanced souls, after reaching the Piscean stage, elect of their own choosing to return to Earth to rescue those still in darkness

  • transformice pirata responded:

    Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post with people.

  • blackwork tattoo responded:

    Great post, and great website. Thanks for the information!

  • miami to key west bus responded:

    When you take stroll on this Miami Beach at night, you will come across several art sculptures.

  • agen sbobet terpercaya responded:

    I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.

  • responded:

    The blog has a to a great degree limit center that amplifies, shockingly into extensive variety of concerns.

  • Jonathan responded:

    That is a very good article in explaining what is a mocking framework, which is not so straightforward to understand (From experience).

    Good job!

  • loemrntdhe responded:

    Yes, I completely agree with the above discussed points. It is a healthy discussion here for learners

  • 20 Questions responded:

    I was taking a gander at some of your posts on this site and I imagine this site is truly enlightening! Keep setting up..

  • alif laila responded:

    This is an awesome motivating article.I am practically satisfied with your great work.You put truly extremely accommodating data. Keep it up. Continue blogging. Hoping to perusing your next post.

  • Chathura responded:

    This actually a nice article and the example used are really helpful.Please continue this work

  • New year wishes photos Free download HD responded:

    I really appreciate this topics you post here. Thanks for sharing us a great information that is actually helpful. New year wishes photos Free download HD

  • AVG free Antivirus responded:

    This blog post really grabbed my attention. With that said I am going to subscribe. Therefore I will get more updates on what you have to say. Please keep writing as I want to learn more. AVG free Antivirus

  • brian holm lawyer san diego responded:

    I read this bit of composing and view it as the quintessential blog.

  • Welger Parts Catalog responded:

    This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Welger Parts Catalog

  • Google Chrome Free Download responded:

    I am happy to find this post very useful for me, as it contains lot of information. I always prefer to read the quality content and this thing I found in you post. Google Chrome Free Download

  • windows 10 yeni kullanıcı responded:

    hanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing...

  • responsive wordpress theme responded:

    I need to to thank you for this very good read!! I definitely loved every little bit of it. I have you bookmarked to check out new things you post…

  • responded:

    Hey, Very NIce post. Loved it. Thanks For sharing this with us.
    Fix Your Technical Problem With Fixingblog. Are you Facing Problem With Your Norton Antivirus? We are Providing free Support For Norton.

  • valentine week list 2018 responded:

    Eventually, the author make an update for a blog. I used to be waiting anxiously for your own next update. I am hoping you will consider updating often so your readers may follow along. I do not have much joy in life today but your blog is one of them. I recognize life is busy but I really hope you will take the time to keep us modified on any progress.valentine week

Add your own comment...