Mock, Mock... who's that? (Part 1)

Let me begin with this quote from Martin Fowler's post :
The term 'Mock Objects' has become a popular one to describe special case objects that mimic real objects for testing. Most language environments now have frameworks that make it easy to create mock objects. What's often not realized, however, is that mock objects are but one form of special case test object, one that enables a different style of testing.
Mocking - huh?
This post series is not to explain / debate on mocking and stubbing test strategies. If you are interested in reading what mocks are, why to use them and so on, please refer to the following links :
If you have decided to take the mock path and are interested in how to use mocks, please read on. Here is my agenda on covering what I've learned from my project's mock testing efforts :
  1. Mocking libraries and comparison. What I've finally used and why
  2. Mocking of instance functions, in a thread safe manner
  3. Mocking of static functions, in a thread safe manner
  4. Mocking of static getInstance() / factory methods, in a thread safe manner
Though my mock usage examples would be in Java, most of the concepts can be used across languages. So, here we go...

Mocking libraries and comparison
There are multiple library projects that offer mocking and stubbing functionality. The number of choices are overwhelming and its often difficult to decide what's the right one to choose. For starters, simple dynamic proxy or sub-classing "mockers" would be just fine. For advances usages, "mockers" with greater benefits - probably state maintenance, static mocking would be useful.

A comparison of currently available mock libraries can be found here :
  1. JMockit documentation recently hosted an exhaustive comparison matrix recently.
  2. A comparison by Vasily Sizov on his blog from March 2009.
  3. StackOverFlow.com users had this great discussion over mock library choices

Our initial set of candidates included EasyMock and Mockito. Though JMockit sounded very tempting, due to lack of documentation (about 4 months back - though its improved quite a lot now) we did not want to adopt it - or so we thought.

EasyMock and Mockito are both very easy to code to and it was a tough call which one to use. Finally, multiple team members got their hands dirty using the two libraries and we voted. The winner was Mockito - we simply loved its documentation (everything was just there) and the extensibility for mocking behaviors. I have been happily mocking using Mockito since then - here I have use and like about Mockit :
  • unlike EasyMock, Mockito is not a "record-n-play" mocker. You mock out what is necessary, when it is necessary, make unit test API calls and then verify expectations - all on demand.
  • behavior mocking is very simple, written like English sentences in code : when(BasicService.foo()).thenReturn(retval);
  • throwing exceptions, tracking number of calls made was possible and easy
  • the return values from mock methods can be customized using the API call parameters.
  • verification of mocked behavior is easy. Though there is no auto-verification feature.
Hope this post will help you find your mocker. I will follow up with more posts on how we used mocks in a thread safe manner, injected them into Spring and how mocking static functions made us use a mix of Mockito and JMockit :). Stay tuned!

    Hilarious Strips from xkcd

    After reading this hilarious suggestion, I fooled around to find some more :

    Zealous Autoconfig

    Unimodal Search

    An array A[1...n] is unimodal if it consists of an increasing sequence followed by a decreasing sequence. More precisely, if there is an index m in {1, 2, ... n} such that
    A[i] < A[i+1] for 1 <= i < m and
    A[i] > A[i+1] for m <= i < n

    In particular, A[m] is the maximum element, and it is the unique "locally maximum" element surrounded by smaller elements.

    Give an algorithm to compute the maximum element of a unimodal input array A[1...n] in O(lg n) time.

    Source: Problem 1-3.from MIT OpenCourseWare Course - 6.046J / 18.410J Introduction to Algorithms (SMA 5503)

    100 Switches & 100 bulbs

    There are 100 switches in a room operating 100 bulbs. At iteration '0' all switches are OFF. For every iteration 'i', all switches that are multiples of 'i' are toggled (turn OFF if ON, turn ON if OFF).

    You need to find the state of the 'k'th switch/bulb (1<=100) after the 'i' th iteration

    Expected O(1) time and space complexity.

    [ALGO] Is Binary Search Tree

    Given a binary tree, verify if it is Binary Search Tree

    [ALGO] Find the string in 2 dimensional matrix

    Given a 2-dim matrix of characters 'm' and a string 's' - find if the string 's' is present in the matrix. Only characters in the neighboring cells of a cell can contribute to the string.

    For example, for the case below B,D,F,G are neighbors of X. From 'X' possible strings (or substrings) are XB, XD, XF, XH.
    |-----|-----|-----|
    |  A  |  B  |  C  |
    |-----|-----|-----|
    |  H  |  X  |  D  |
    |-----|-----|-----|
    |  G  |  F  |  E  |
    |-----|-----|-----|

    Finally, an example. Given the matrix

    (0,0)
        |-----|-----|-----|-----|
        |  y  |  a  |  a  |  o  |
        |-----|-----|-----|-----|
        |  a  |  o  |  a  |  o  |
        |-----|-----|-----|-----|
        |  y  |  a  |  o  |  o  |
        |-----|-----|-----|-----|
        |  h  |  a  |  a  |  o  |
        |-----|-----|-----|-----|
        |  y  |  a  |  h  |  o  |
        |-----|-----|-----|-----|
                                (3,3)

    and asked to check for the string 'yahoo' the solution is true :
    y - (3,0)
    a - (3,1)
    h - (3,2)
    o - (3,3)
    o - (2,3)

    [ALGO] Random number generator, with equal probability

    RANDOM(a.b) is a random number generator that generates integers between a and b (inclusive), all equally likely.

    Assuming we have an implementation of RANDOM(0.1), how can we implement RANDOM(a.b) - i.e. given we have a function that returns 0 OR 1 both with a probability of 1/2, how can we implement a function that returns integers from a to b (b > a), all with a probability of 1/n, where n = (b-a+1)

    Source : Cormen - Problem 5.1-2 (randomized algorithms)


    Similar problem :
    At a restaurant, how can Veronica choose one out of three desserts with equal probability with the help of a coin?

    Its on Gurmeet's site here  http://gurmeetsingh.wordpress.com/2008/09/12/puzzle-tossing-with-one-third-probability/
     
    Moviehole-madhurtanwani © 2010 | Designed by Chica Blogger | Back to top