When working with legacy code, subsequent covering with unit tests is not an easy task. Quite often one is faced with the dilemma of refactoring - you need tests for safely refactorings, but the code needs to be refactored first in order to write any tests.
Bringing existing code in better shape is essential when existing code must be covered with unit tests. In this presentation we focus on refactorings that are targeted for covering legacy code with unit tests, after it has been written without tesing in mind.
This is the handout for the presentation. See also:
- Slides: http://www.slideshare.net/rfilipov/refactoring-for-tests-0lides
- Sources: https://github.com/rusio/refactoring-for-tests
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Refactoring for Tests - Handout
1. public class Original { // Coupling: Static Dependencies 1
public void usage() {
BookingService bookingService = new BookingService();
bookingService.bookConference("Big Trouble in Little China");
}
}
public class BookingService {
public BookingResult bookConference(String topic) {
ConferencingServer conferencingServer = new ConferencingServer();
MeetingCalendar meetingCalendar = MeetingCalendar.getInstance();
Date startDate = meetingCalendar.nextPossibleDate();
try {
return conferencingServer.bookConference(topic, startDate);
}
catch (BookingException e) {
return BookingResult.forFailure(e);
}
}
}
class ConferencingServer {
public BookingResult bookConference(String topic, Date startDate) throws
BookingException {
// NOTE: real implementation would connect to
// the conferencing server and book the conference
return BookingResult.forSuccess("0721/480848-000", startDate);
}
}
class MeetingCalendar {
private static final MeetingCalendar instance = new MeetingCalendar();
private MeetingCalendar() {
// enforce Singleton
}
public static MeetingCalendar getInstance() {
2. return instance;
}
public Date nextPossibleDate() {
// NOTE: real implementation would look up some
// calendar database and calculate the date
return new Date();
}
}
public final class BookingResult {
private final BookingException errorCause;
private final String phoneNumber;
private final Date startDate;
private BookingResult(BookingException errorCause,
String phoneNumber,
Date startDate) {
this.errorCause = errorCause;
this.phoneNumber = phoneNumber;
this.startDate = startDate;
}
public static BookingResult forSuccess(String phoneNumber, Date startDate) {
return new BookingResult(null, phoneNumber, startDate);
}
public static BookingResult forFailure(BookingException error) {
return new BookingResult(error, null, null);
}
public boolean isSuccess() { return errorCause == null; }
public String getPhoneNumber() { return phoneNumber; }
public Date getStartDate() { return startDate; }
public BookingException getErrorCause() { return errorCause; }
}
3. public class Original { // Coupling: Static Dependencies 2
public void usage() throws BookingException {
// NOTE: the real implementation would create
// these objects at a central place.
BigFatContext bigFatContext = new BigFatContext();
BookingService bookingService = new BookingService(bigFatContext);
bookingService.bookConference("Big Trouble in Little China");
}
}
/**
* NOTE: imagine it is difficult to instantiate this
* class and its collaborators in a test harness.
*/
public final class BigFatContext extends FrameworkClass {
private final Map<String, Object> services = new HashMap<String, Object>();
public BigFatContext() {
// NOTE: the map contains real objects, which are created here
// => how to replace these two with mock objects in a test?
services.put("conferencingServer", new ConferencingServerImpl());
services.put("meetingCalendar", new MeetingCalendarImpl());
}
public ConferencingServer getConferencingServer() {
return (ConferencingServer) services.get("conferencingServer");
}
public MeetingCalendar getMeetingCalendar() {
return (MeetingCalendar) services.get("meetingCalendar");
}
}
public class BookingService {
private final BigFatContext bigFatContext;
public BookingService(BigFatContext bigFatContext) {
this.bigFatContext = bigFatContext;
}