TDD and mocks

First of all I must remind you that I don't like mocks and mocks aren't stubs. But recently I read a very interesting story on code without getters and its consequences. The story is about a company where the developers where asked to develop their (OO) code without any getters. The brilliance of this simple, and at first glance weird constraint is that it forces the developers to design the code in a way that is generally considered "better" (i.e. don't ask objects for data, tell them what to do). It also introduces the need for mocks in a natural way. Actually mocks are not needed. A fake or stub object might work just as well.

Let's take a look at an example. If we look at my example from I don't like mocks where I want to transfer funds from one account to another. Without getters the code would have to look something like this:

 transaction.commit(accountFrom.remove(amount), accountTo.add(amount))

If we use a mock object we can test that this code actually changes the account values correctly. But we still have the same maintainability and focus problems I discussed in the I don't like mocks post earlier. And you might as well use a stub or fake object. Actually the use of a stub and fake will force you to focus on testing the right things in your unit test because you have to test the account.remove and account.add methods by them selfs. And when you know they work you can fake/stub that unit completely and start testing that transaction.commit does the right thing given different behavior from the account objects.