Pragmatic Unit Testing in Java with JUnit

Front Cover
Pragmatic Bookshelf, 2003 - Computers - 159 pages

Learn how to improve your Java coding skills using unit testing. Despite it's name, unit testing is really a coding technique, not a testing technique. Unit testing is done by programmers, for programmers. It's primarily for our benefit: we get improved confidence in our code, better ability to make deadlines, less time spent in the debugger, and less time beating on the code to make it work correctly.

This book shows how to write tests, but more importantly, it goes where other books fear to tread and gives you concrete advice and examples of what to test--the common things that go wrong in all of our programs. Discover the tricky hiding places where bugs breed, and how to catch them using the freely available JUnit framework. It's easy to learn how to think of all the things in your code that are likely to break. We'll show you how with helpful mnemonics, summarized in a handy tip sheet (also available from our www.pragmaticprogrammer.com website) to help you remember all this stuff.

With this book you will:


  • Write better code, and take less time to write it
  • Discover the tricky places where bugs breed
  • Learn how to think of all the things that could go wrong
  • Test individual pieces of code without having to include the whole project
  • Test effectively with the whole team
We'll also cover how to use Mock Objects for testing, how to write high quality test code, and how to use unit testing to improve your design skills. We'll show you frequent "gotchas"--along with the fixes--to save you time when problems come up. We'll show you how with helpful mnemonics, summarized in a handy tip sheet (also available from our www.pragmaticprogrammer.com website).

But the best part is that you don't need a sweeping mandate to change your whole team or your whole company. You don't need to adopt Extreme Programming or Test-Driven Development, or change your development process in order to reap the proven benefits of unit testing. You can start unit testing, the pragmatic way, right away.

From inside the book

What people are saying - Write a review

User ratings

5 stars
1
4 stars
4
3 stars
0
2 stars
0
1 star
0

User Review - Flag as inappropriate

读书笔记:
Pragmatic Unit Testing In Java with JUnit
这是一本讲Unit Testing的好书.尽管对Junit的讲述有点过时.毕竟是2003-04的老书了.
Unit Testing对一名Software Engineer的重要性不言而喻了.也不必多讲.
而Unit Testing工具的模板也到处都有,很好理解,无非就是Testcase, TestSuite, testEquals之类的.
这本书值得记录的重点是关于对Unit Testing的范围的界定.
第四章, What to Test: The Right-BICEP给出了很好的界定.
Right - Are the results Right?
确保代码运行的结果正确.
B - BOUNDARY conditions
确保代码在极端条件下正确.
比如,奇怪的字符串,格式错误的邮件地址,该输入整数的地方变浮点数或空格,年龄一万岁,序列的拷贝,有序数列,参数顺序颠倒,多出或缺失.
这里又有一个参考的列表, CORRECT, 第五章又用单独一章专门讲了这点.
Conformance - value conform to an expected format
错误的,奇怪的数据格式.比如电子邮件有过多后缀,或没有@符号,等等.
Ordering - odered/unordered as appropriate
确保数据序列是排序的.
Range - reasonable minimum/maximum value
比如人的年龄.
Reference - code reference anything external, out of control of code itself.
当代码需要依赖别的组件的时候.
Existence - value exist? non-null, non-zero, in a set/enum
空对象,空格和其他空白字符,或0值.
Cardinality - exactly enough values?
0-1-n Role, 需要保证至少这3种情况能通过测试.
Time - happen in order? right time? In time?
绝对和相对时间,线程同步时的问题. 以及夏时制.
I - check INVERSE relationships
反向测试,
比如设计个求平方根的函数,就用它算出来的结果求平方和原来的值比较,
或者设计了往数据库写入数据,就用查询方法查询是否已经写入.
C - CROSS-CHECK using other means
用另外一种不同的方法或者算法,重新实现过程并得到结果,比较两种结果是否相同.
E - force ERROR CONDITIONS
用Mock Objects的方法模拟不可避免的客观条件错误.
这里举了一些例子:
内存耗尽
硬盘空间耗尽
系统时间错误或不正常
网络可用性和错误
系统负载
有限的色彩模式,比如256色的屏幕
过高或过低的显示器分辨率
P - PERFORMANCE characteristics
测试性能需要相对的参数指标, 要考虑10,000甚至100,000数量级的情况.
例子的办法是在运行要测试的方法前后开启和停止一个计时器. 然后比较计时器的时间和预先设定的容忍时间,论秒的.
 

LibraryThing Review

User Review  - bryan.miller.3705 - LibraryThing

after reading this book, I'm convinced unit testing is essential in all but the most trivial code projects. Read full review

Contents

Preface
1
Tour First Unit Tests
13
Writing Tests in JUnit
21
Copyright

10 other sections not shown

Other editions - View all

Common terms and phrases

About the author (2003)

Andrew Hunt is an experienced examiner and educator.

David Thomas, Ph.D., is the current co-director of The Bethany Family Institute. For over thirty years he was a Professor of Systematic Theology, Religion, and Family Life at St. Louis University, St. Meinrad School of Theology in Indiana, and Regis University in Denver. Thomas served as a theological consultant to the United States Catholic Bishops' Committee on Marriage and Family Life. He and Karen, his wife of forty years, are the parents of seven children and seventy-five foster children. He now lives in Whitefish, Montana.

Bibliographic information