Unit Testing
Unit Testing
____________________________
Vocabulary, styles, toys
Vocabulary
____________________________
JUnit framework
“Unit tests are tests written with JUnit” (naive, simplistic, false)
“One of the challenges of unit testing is to sufficiently decouple the multiple units of code that make up
an application so that each unit can be tested individually.”
Testing simple functionality (ex: new methods) in Integration Tests can lead
to:
Fragile Tests
Unrepeatable Tests
…
Maintenance Nightmare
Styles
____________________________
Using Test Doubles
Non trivial tests:
• Large number of dependencies
• Awkward collaborations
• Complex Objects How to isolate the units of code?
Test Doubles :
• Dummy - objects are passed around but never actually used. Usually they are just used to fill
parameter lists.
• Fake - objects actually have working implementations, but usually take some shortcut which
makes them not suitable for production
• Stubs - provide canned answers to calls made during the test, usually not responding at all to
anything outside what's programmed in for the test.
• Mocks - objects pre-programmed with expectations which form a specification of the calls
What:
Order : must fill itself from Warehouse
If Warehouse contains the product quantity requested by Order:
- Order becomes filled, Warehouse quantity adjusted.
If Warehouse does not contain necessary quantity:
-Order does not become filled. Warehouse unchanged.
With a Stub:
Styles
____________________________
Mocks
“Once,” said the Mock Turtle at last, with a deep sigh, “I was a real Turtle.”
(Alice In Wonderland, Lewis Carroll)
Styles
____________________________
Mock test: jMock
Styles
____________________________
We’re late!
Vocabulary
____________________________
State-based testing
• Setup in a different place - a stub may be shared, it’s returned values not visible in
the test
• Sometimes stubs may be hard to implement by hand – how do we stub a
HttpServletRequest? A java.io.File? What do we put in constructors?
…
Vocabulary
____________________________
Interaction-based testing
• In TDD -> leads to good design (Interface discovery, Tell Don’t Ask)
….
Vocabulary
____________________________
What should I use?
• Some code is very stateful, and is best tested with a state-based test. .
• Other code is more stateless and can be fully tested only with an interaction-based
style
• Design phase – mocks may help
“Uncle Bob” suggests the F.I.R.S.T acronym in “Clean Code” to describe what well written (clean) unit tests should
look like:
Fast - they should run quickly. This encourages you to run them often. (framework)
Independent - they should not depend on each other. It should be possible to run the tests in any order.
Repeatable - it should be possible to run them in any environment. They should be environment
independent.
Self-Validating - they should either pass or fail. (framework)
Timely - they should be written in a timely manner i.e. just before the production code is written.
Toys
____________________________
Mockito
questions?
____________________________
Martin Fowler: “Refactoring. Improving the design of existing code”
Robert “Uncle Bob” Martin: “Clean Code: A Handbook of Agile Software Craftsmanship”
http://martinfowler.com/articles/mocksArentStubs.html
http://mockobjects.com/
http://benpryor.com/blog/2007/01/16/state-based-vs-interaction-based-unit-testing/
http://xunitpatterns.com/Mocks,%20Fakes,%20Stubs%20and%20Dummies.html
http://blogs.thoughtworks.com/
http://code.google.com/p/mockito/