星期日, 4月 25, 2004

Some experiences with unit test...... and Object Mother Pattern

之前 JUnit in action 唸了半本,當時想想這樣開發程式真是完美啊!所有的 component 都是用 interface 隔的一乾二淨的。然後每個 component 跟其他的 component 之間 都是用 IOC (Inversion of Control) 設定。這樣開發的時候就可以用一堆 mock,而不用再準備一大堆的 test fixture。起初唸完還興奮的不得了說....... 不過直到最近..... 開始真正使用 Junit 之後,惡夢才真的開始!

惡夢中體會到的:

  • Not all components need to apply interface ! 如果幻想每個 component 都用 interface.... 那就會有管不完的 interfaces !不暈倒才怪!應該是只有環境 (例如 file system, connection.... etc) 以及各子系統間才需要額外添加 interface.
  • For mass test fixture, Use Object Mother Pattern ! 這個 pattern 真是棒!不僅可以管理/重用辛辛苦苦建立的 test fixture ,也可以有效的清掉(teardown)建立的 fixture. 而且發現搭配 Hibernate 後使用起來更是爽!
  • I don't write any code today !! 終於!我也犯了全天下 unit tester 都會犯的錯。居然一整天都在 write/refactor test case, fixture, 還有那個 Object 媽媽...... 媽媽咪啊!Business code 一行也沒寫到!