Ralf Wehner's Blog

Just another WordPress.com weblog

Posts Tagged ‘java

Mockito – some programming examples

with 5 comments

My favorite mocking library in JAVA is Mockito. It is absolutely great!

So, in this post i want to show some programming examples how to use Mockito in your JUnit test code. I don’t want to write a tutorial or a howto, this is just a summary of programming examples for JAVA programmers who are familiar with Mockito.

Capturing method arguments from Mocks for further assertions:
If you are using the version 1.8 and above you can use the ArgumentCaptor. The ArgumentCapture gives you a way to get one or more Arguments of a mocked method that was called before. Use the ArgumentCapture only if need to evaluate complex objects or objects that were created outside your junit test code. If you just want to verify the proper call of a simple mocked method, its better to use a simple verify(mock).yourMethod(). This makes your test code more readable.

So, start working with ArgumentCaptures with code:

package de.wehner.mockito.samples;

import static org.junit.Assert.assertEquals;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;

import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
public class MockitoSampleTests {

    @Test
    public void testInvideJohn() {
        ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
        Party party = mock(Party.class);

        Person john = new Person("John");
        Person peter = new Person("Peter");

        // Peter invites John to the party
        peter.invideToParty(john, party);

        verify(party).addGuest(argument.capture());
        // verify John was invited
        assertEquals("John", argument.getValue().getName());
    }
}

And this is the Person:

package de.wehner.mockito.samples;

public class Person {

    private final String _name;

    public Person(String name) {
        super();
        _name = name;
    }

    public String getName() {
        return _name;
    }

    public void invideToParty(Person friend, Party party) {
        party.addGuest(friend);
    }
}

Another way to solve this problem is to use a helper class (here this this the Holder<>) that is final and must be used in combination with the BaseMatcher<> class from Mockito.

package de.wehner.mockito.samples;

import static org.junit.Assert.assertEquals;

import static org.mockito.Matchers.*;

import static org.mockito.Mockito.*;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.core.AnyOf;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

public class MockitoSampleTests {

    @Test
    public void testInvideJohn() {
        ...
    }

    public class Holder<T> {
        private T _value;

        public Holder() {
        }

        public Holder(T value) {
            _value = value;
        }

        public T get() {
            return _value;
        }

        public void set(T value) {
            _value = value;
        }
    }

    @Test
    public void testInvideJaneAndJohn() {
        Party party = mock(Party.class);
        Person peter = new Person("Peter");
        Person jane = new Person("Jane");
        Person john = new Person("John");

        // "listen" to invocations of Party.addGuest() method
        final Holder<Person> lastInvited = new Holder<Person>();
        doNothing().when(party).addGuest(argThat(new BaseMatcher<Person>() {

            @Override
            public boolean matches(Object arg0) {
                lastInvited.set((Person) arg0);
                return true;
            }

            @Override
            public void describeTo(Description arg0) {
            }
        }));

        // Peter invites his friends
        peter.invideToParty(jane, party);
        peter.invideToParty(john, party);

        // verify 2 guests are invited and John is the last one
        verify(party, times(2)).addGuest(any(Person.class));
        assertEquals(john, lastInvited.get());
    }
}

Written by rwehner

February 23, 2010 at 9:56 pm

Posted in java, JUnit

Tagged with , ,