I just wrote my first test with a Mockito mock, and I got confused for just a second writing it.  First up is import syntax:

    import static org.mockito.Mock.*;

That static specifier is key.  They tell you that right up front, but if you are (as I am) relatively new to Java it’s still a little bit of a trick.

Also in the syntax, this is really not something I’m used to seeing coming from .NET:

    Thing thing = mock(Thing.class);

Shades of C, Batman!  That’s quite an interesting little linguistic trick that I haven’t seen on the other side of the fence.

The last thing I ran into was just a duh moment.  As I mentioned before, I’m using one-assertion-per-test TDD, so when I wanted to write this:

    public static final String TEST_VALUE = "";
    Dependency mocked;
    Dependent testObject;

@Before public void setUp() { mocked = mock(Dependency.class); when(mocked.callMethod(param)).thenReturn(TEST_VALUE); testObject = new Dependent(mocked); testObject.callMethodUnderTest(); } @Test public void testCallMethod() { verify(thing).callMethod(param); Assert.assertEquals(testObject.receivedValue(TEST_VALUE)); }

my stomach kind of dropped.  There are two conditions in there that could fail.  I know it’s being a bit purist to want to break that down, but it’s also pretty simple to do so.  All I ended up doing, which nicely separates concerns, is this:

    @Test public void dependencyCalled() {
        verify(thing).callMethod(param);
    }

    @Test public void dependencyReturnValueUsed() {
        Assert.assertEquals(testObject.receivedValue(TEST_VALUE));
    }

It’s so simple, but it was very satisfying once I uncrossed my brain enough to spot the solution.