When Mockito’s InjectMocks does not inject mocks

5 comments

Thursday, November 26, 2015

Looking back at the time wasted yesterday while trying to make a trivial functionality work makes me wonder if it was a good idea to begin with…
It all started  (and ended) yesterday while I was mentoring a team on the fine art of Java unit testing.
We’ve decided to use Mockito’s InjectMocks due to the fact that most of the project's classes used Spring to fill private fields (don’t get me started).
For those of you who never used InjectMocks before – in the Mockito word we can auto-magically initialize and inject mock objects into the class under test. And it’s all done using annotations.
And so if I have the following class:

public class MyClass {

    @Resource
    private INetworkService networkService;

    @Resource
    private IFileService fileService;

    public boolean SomeMethod(){
        // some logic here

        // More logic here
        networkService.Send();

        return true;
    }
}

I can write a test fixture that looks like this:

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
    @Mock
    private INetworkService networkService;

    @Mock
    private IFileService fileService;

    @InjectMocks
    MyClass myClass;

    @Test
    public void testSomeMethod() {
        boolean result = myClass.SomeMethod();

        assertTrue(result);
    }
}

And so the two dependencies (marked with @Mock) would be faked and inserted into MyClass by constructor, property or field injection.
The problem was that we couldn’t get it to work. The mocks were initialized and the class was created - without the dependencies. It took us some time but finally we’ve found the reason: the real class looked something like this - can you spot the difference?

public class MyClass {

    @Resource
    private INetworkService networkService;

    @Resource
    private IFileService fileService;

    private Integer version;

    public MyClass(Integer version) {
        this.version = version;
    }

    public boolean SomeMethod(){
        // some logic here

        // More logic here
        networkService.Send();

        return true;
    }
}

Did you see it?
In our real class we had a non-empty constructor which InjectMocks tried to use, but passed null since Integer cannot be mocked by Mockito (it’s a final class). Once Mockito found a constructor to try and to use it didn’t even try to inject the two fields (lazy bastard). And so the dependencies inside the MyClass remained null causing a null reference exception to be thrown once used.
It’s not that Mockito guys didn’t do a good job, this behavior is documented – which makes it yet another case of RTFM.
The problem is that the tests who successfully run using this mechanism could one day fail just because someone decided to add another constructor…

Happy coding

I’m going to have a good time in the next couple of months – I have several interesting speaking opportunities right around the corner:

agileTD

In less than two days I’ll be traveling to Germany to attend AgileTD. This is my first time at that AgileTD but from what I hear it’s an amazing conference. This year there are several ATDD/BDD talks which I plan on attending – as well as contribute to, since on the last day I get to speak about A/T/B/DD in Navigating the xDD alphabet soup.

codcemotion

The next conference is on my home turf – CodeMotion is coming to Tel Aviv and I get to speak about unit testing mistakes and misunderstanding in 5 unit testing facts I wish I know 7 years ago.

ndclondon

This is the third time I get invited to speak at NDC London – one of the best conference I got to speak at with excellent speakers. This time I have something different in store - I’m going to do a session on Electronics 101 for software developers.

 

That’s it for now – wish me luck, and if you’re around – come over and say hello.

Related Posts Plugin for WordPress, Blogger...