Skip to content

Instantly share code, notes, and snippets.

@unclejamal
Created June 30, 2014 23:02
Show Gist options
  • Save unclejamal/10fdd1c7790733fc57e1 to your computer and use it in GitHub Desktop.
Save unclejamal/10fdd1c7790733fc57e1 to your computer and use it in GitHub Desktop.
Here's what I meant when I said tests can break even if you only move a method to a different class.
This was the code before we started refactoring:
class BreadShop { ...
public void deposit(int accountId, int creditAmount) {
Account account = accountRepository.getAccount(accountId);
if (account != null) {
final int newBalance = account.deposit(creditAmount);
events.newAccountBalance(accountId, newBalance);
} else {
events.accountNotFound(accountId);
}
}
}
We moved the logic to the AccountRepository class (such that BreadShop followed TellDontAsk):
class BreadShop { ...
public void deposit(int accountId, int creditAmount) {
Account account = accountRepository.deposit(accountId, creditAmount, events);
}
}
Now, if the tests had been written using the London School of TDD (a.k.a. the mockist approach of TDD, a.k.a. the isolation school of TDD :)), they would have looked something like this:
class BreadShopTest {
public static final int creditAmount = 200;
public static final int newBalance = 100;
public static final int accountId = 1;
public void depositsMoneyToExistingAccount() {
Account existingAccount = Mockito.mock(Account.class);
when(existingAccount.deposit(creditAmount)).thenReturn(newBalance);
AccountRepository repository = Mockito.mock(AccountRepository.class);
when(repository.getAccount(accountId)).thenReturn(existingAccount);
new BreadShop(repository).deposit(accountId, creditAmount);
verify(events).newAccountBalance(newBalance); // CRITICAL INTERACTION
}
}
Of course, after the refactor we didn't have this interaction happen in this class, so the test would have broken.
Now, I know that you work with the classical TDD, so it wouldn't ever happen to your code. My point was simply that we didn't know the code base we were given, so had the tests been written with the mockist approach, they would have been broken after our Move Method refactoring.
Anyways, thanks for that pairing session again. It was certainly lots of fun :)
@NicoleRauch
Copy link

OK, understood!
Thanks for reminding me that mocking (as opposed to stubbing) is so very common around here ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment