Pages

Tuesday, August 18, 2009

Know your SUT and your mocks

I was working with a colleague of mine tonight and he became confused with his unit test. It was an interesting exchange, so I thought I would write it up. He had a system under test (SUT), a service, that had a number of dependencies. The dependencies were mocked using a mocking framework (mockito in this instance). He was writing a test to get better code coverage in preparation for some refactoring and he started to add an expectation to the SUT. When he ran his test, the test failed, but the error lead you to believe that it was the improper use of matchers within mockito. It took him and I awhile to realize that he was trying to add expectations to a non-mock class. In hindsight, he should have picked up on this right away, as we have a coding convention of naming our mock object instances as mock*. The SUT does not follow this naming convention and hence you should see the problem right away. A little concerned that mockito didn't alert us to the fact that we were trying to add expectations to a non-mock instance. Moral of the story: Make your mock object instances stand out--name them appropriately so everyone knows that it's a mock.

No comments:

Post a Comment