Posts Tagged ‘unti testing’

Endo-Testing: Discussion

December 16, 2008

I read Dan North’s blog the-end-of-endotesting sometime ago and have been meaning to write up a response of my own for sometime.

The thing about it I initially found confusing was the reference to endo-testing. It would seem that he believed that using the tool Mockito instead of other expect-run-verify mocking frameworks meant that you weren’t doing what was described as endo-testing in [1].

Up until now I have used JMock as my mocking framework of choice. I had read of Mockito and intended to take it for a test drive but until now I just haven’t found the time. My assumptions (from what I had read so far) about Mockito were that it used a test spy as a test double whereas mocking frameworks such as EasyMock and JMock were using a mock as the test double for the depended-on-component.

Note: I am using the terminology Gerard Meszaros describes in [2].

The difference between these approaches is the difference between observation and expectation. The observation approach of a test spy allows for us to exercise the test and then make assertions about what did happen to our test double (test spy) whereas the expectation approach requires us to make expectations about what will happen to our test double (mock) before we exercise the test.

In summary

In summary I think that both approaches can be classified as what was coined endo-testing in [1].

When writing unit tests its important that we know the difference between the types of test doubles described in [2] and know when its best to use them (for the sake of the test).

When I first used JMock, it felt weird that the behaviour verification part of the test had to be implemented (before the exercising of the method under test). When writing tests it does feel more natural to do assertions about what did happen whether those assertions are to do with the state of the system under test (SUT) or assertions to do with what interaction occurred between the SUT and the test double.

So I will also be converting from JMock to using Mockito for my unit tests. There is still a place for the the expect-run-verify mocking frameworks such as EasyMock and JMock but I think that most tests that I am required to write will be benefit from using Mockito.

In another blog I hope to discuss reasons why I am going to use Mockito as opposed to JMock. State-based testing, interaction-based testing, TDD, and classical TDD versus Mockist TDD approaches will most likely come up in the discussion.


[1] Endo-Testing: Unit Testing with Mock Objects; Mackinnon, Freeman and Craig
[2] xUnit Test Patterns; Gerard Meszaros