Wednesday, October 8, 2008

JUnit design

JUnit design

It started a while ago when I read Jay Fields’ inspiring post about being your own start-up where he mentioned two books one should read. One of the books was My Job Went to India. Despite its weird name, it is a great writing with honest pieces of advice how to be a better developer. After reading and truly enjoying it, I realized the worst thing can happen now is that I mark it as “read” on the list and move on. So I decided to follow some of the ideas from “act on it”-parts. One of them is to find new ideas and inspiration from projects that are generally considered good.

Here is just a high-level overview of how junit works in my over-simplified interpretation in case I loose a piece of paper I drew it on :) For more substantial overview of junit 3.8, take a look at cooks’ tour or excellent paper by Dirk Riehle.

JUnit 3.8

junit 3.8 overview

Given a class TestRunner tries to use it as a TestSuite. If it’s not a TestSuite, TestRunner creates TestSuite with TestCases for each test method in this class. TestRunner creates TestResult object which collects results of running tests and passes them to TestListeners. Tests notify TestResult which passes events to TestListeners. TestRunners bundled with junit are also TestListeners since they subclass BaseTestRunner.

JUnit 4.x

junit 4.x overview

Given a class JUnitCore creates a Request. Request decides which Runner to use for executing tests (this is where @RunWith annotation is used; see Mark Burnett’s post for example of how to create custom Runner). Runner finds test methods and executes them. For each test Runner creates Description which is sent to RunNotifier and then passed to RunListener. After tests are executed JUnitCore sends Result to RunNotifier and RunListener.

0 comments: