Testing and Refactoring
- Length of workshop: 5 days
- General Overview:
In this workshop, you'll learn just how brilliantly effective test-driven development can be, how to write good test code and how to practice the rhythm of refactoring. While its name implies that test-driven development is mostly about testing, it is primarily about design: it keeps you focused on what exactly you need to build and helps you avoid over-engineering.
Refactoring is a practice for improving your code's design, without adding new functionality by performing behavior-preserving transformations. By making small design improvements, refactoring helps you remove duplication (a nesting ground for bugs), clarify your code's intention (so others can understand it) and simplify your logic.
Automated Testing is a way to quickly ensure that your code does what you and your customers expect it to do. Testing best practices include layering your application for testability, using data and frameworks to drive tests, scripting and pre-loading data into a database and running tests simultaneously for maximum speed.
- Leader: Joshua Kerievsky
- Workshop Goals:
This workshop provided a hands-on opportunity to practice test-driven development in Java (or your object-oriented language of choice); write tests using JUnit or equivalent Xunit; refactor code and learn to identify code that needs to be refactored; learn what Mock Objects are and how/when to use them; study and learn the refactoring catalog; write automated black-box tests and frameworks for testing; learn how to use XML in testing; test web pages using HttpUnit; use automated refactorings in the Eclipse IDE; practice refactoring to patterns.
- Intended Audience: This workshop in limited to a maximum of 16 participants.
- Outline/At a Glance:
Much of what you'll do in this workshop is write code. When you're not writing code, you'll be studying and implementing refactorings, getting experience with test-driven development, participating in interactive demos (such as the refactoring fishbowl) and learning about automated testing techniques. This workshop is taught using Java or .NET and can be customized to meet your specific needs.
Day 1: After learning the basics of JUnit (or equivalent XUnit) and a half-dozen simple refactorings, you'll begin your first coding exercise: to apply test-driven development on a small utility class. Then you'll learn more about the unit testing framework we'll use (JUnit, NUnit, etc.) and how best to code unit tests. Next, you'll immediately put your new knowledge to work in more exercises, in which you'll refactor some smelly code that we provide and extend this code by practicing test-driven development.
Day 2: You'll begin by studying the transformation of a small program from being not very testable and not very well designed to being easy to test and better designed. Next, you'll participate in a dialogue about automated testing accompanied by a demonstration. Following this, you'll practice writing tests for the program you studied. Afterwards, you'll learn a dozen more refactorings and get a chance to practice implementing them and other refactorings using automated refactoring in Eclipse.
Day 3: You'll start the day by studying HttpUnit and learn how to use it to test web pages. Following this, you'll write HttpUnit tests for web pages and review best practices for online testing. Next, you'll learn how to use XML in testing and discover ways to layer your system to support testability. You'll study an XML framework that we developed to help us write tests for our clients and you'll get a chance to use this framework in an exercise. You'll finish the day by exploring a framework that will let XML drive your automated tests.
Day 4: You'll begin by studying a dozen more refactorings, accompanied by exercises. Next, you'll learn about ways to work with your database to support testing, including writing scripts to regenerate tables, seed your database with test data and keep your database clean during testing. This will lead to a discussion of Mock Objects: what they are, how and when to use them. Following this, you'll do two exercises: one in which you add Mock Objects to test code you've already written and one in which you add Mock objects for a legacy system. You'll conclude the day with a general discussion of testing best practices.
Day 5: You'll begin the day by studying a growing body of refactorings to patterns, which illustrate a sensible way to work with patterns to keep your code small, understandable and testable. Following this, you'll implement a refactoring to the State pattern. This will be followed by a short debrief. Next, you'll study a half-dozen more refactorings, followed by a review of all the refactorings you've learned in the workshop. The workshop concludes with a dialogue on how to integrate testing and refactoring into your process, workplace and codebase.
- Prerequisites:
Participants in this workshop must be comfortable with Java or a .NET language and the basics of XML
- Bio: http://www.cutter.com/meet-our-experts/kerievskyj.html
