FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
How to Start Test-Driven Development in Legacy Code
1. How to Start Test-Driven
Development in Legacy Code
The New York XP & Agile Meetup
May 17, 2012
Daniel Wellman
Director of Technology
Tweet
#legacytdd
#xpnyc
28. public class TestCase {
public void run() {
TestResult result = new TestResult();
try {
setUp();
runTest(result);
} catch (Exception e) {
result.addFailure(e, this);
}
tearDown();
}
}
29. public class TestCase {
public void run() {
TestResult result = new TestResult();
try {
setUp();
runTest(result);
} catch (Exception e) {
result.addFailure(e, this);
}
tearDown();
}
}
30. public class TestCase {
public void run() {
TestResult result = new TestResult();
try {
setUp();
runTest(result);
} catch (Exception e) {
result.addFailure(e, this);
}
tearDown();
@Test
}
} public void exceptionsReportAsFailures() {
TestCase testCase = new TestCase();
testCase.run();
// ...??
}
31. After Extract Parameter
public class TestCase {
public void run(TestResult testResult) {
TestResult result = testResult;
try {
setUp();
runTest(result);
} catch (Exception e) {
result.addFailure(e, this);
}
tearDown();
}
}
32. After Extract Parameter
public class TestCase {
public void run(TestResult testResult) {
TestResult result = testResult;
try {
setUp();
runTest(result);
@Test } catch (Exception e) {
result.addFailure(e, this);
public void exceptionsReportAsFailures() {
}
TestCase testCase = new TestCase();
tearDown();
TestResult result = new TestResult();
} testCase.run(result);
} // Make assertions on result
}
39. public class SMTPMailServer {
public void helo(String hostname) {}
public void from(String address) {}
public void size(long byteCount) {}
public void data() {}
public void send(Message message) {}
public void quit() {}
...
private int sendBytes(byte[] toSend) {}
...
}
40. Override Methods
public class FakeOverrideSMTPMailServer
extends SMTPMailServer {
public byte[] sent; // public!
@Override
protected int sendBytes(byte[] toSend) {
// Save the bytes for later inspection
// ...
return 0; // Whatever...
}
}
41. Override Methods
public class FakeOverrideSMTPMailServer
extends SMTPMailServer {
public byte[] sent; // public!
@Override
protected int sendBytes(byte[] toSend) {
// Save the bytes for later inspection
// ...
return 0; // Whatever...
public class SMTPMailServer {
}
} // ...
protected int sendBytes(byte[] toSend) { } ...
}
42. Extract Interface
public interface MailServer {
public void helo(String hostname);
public void send(Message message);
public void quit();
}
43. Extract Interface
sh
-i
public interface MailServer {
public void helo(String hostname);
public void send(Message message);
public void quit();
}
44. Extract Interface
sh
-i
public interface MailServer {
public void helo(String hostname);
public void send(Message message);
public void quit();
}
public class SMTPMailServer implements MailServer {
// Retro-fitted production...
}
45. Extract Interface
sh
-i
public interface MailServer {
public void helo(String hostname);
public void send(Message message);
public void quit();
}
public class SMTPMailServer implements MailServer {
// Retro-fitted production...
}
public class FakeMailServer implements MailServer {
// Testing ...
}
47. public Duration vacationRemaining(Date today) {
Duration remaining;
// ...
// ...
// Calculate hours worked
// ...
// ...
// How much vacation do they get?
// ...
// ...
// Is it a leap year?
// ...
// ...
return remaining;
}
48. Extract Methods
public Time hoursWorked(Time start, Time end) {
// ...
}
public int vacationDaysAllowed(EmployeeType type) {
// ...
}
public boolean isLeapMonth(Date date) {
// ...
}
50. Sprout Method and
Sprout Class
“sprouting bean” photo by Zoë Jackson
51. public class EmployeeTable {
private List<Employee> employees;
public String table() {
StringBuilder html = new StringBuilder();
html.append("<table>");
for (Employee employee : employees) {
html.append("<tr>");
// Print employee data ...
html.append("</tr>");
}
html.append("</table>");
return html.toString();
}
}
52. public class EmployeeTable {
private List<Employee> employees;
public String table() {
StringBuilder html = new StringBuilder();
html.append("<table>");
for (Employee employee : employees) {
html.append("<tr>");
// Print employee data ...
html.append("</tr>"); TODO:
} Show count
html.append("</table>");
of full-time
return html.toString();
} employees
}
53. Sprout Class
public class FullTimeEmployeeCount {
private List<Employee> employees;
public FullTimeEmployeeCount(
List<Employee> employees) {
this.employees = employees;
}
public int count() {
int count = 0;
for (Employee employee : employees) {
if (employee.isFullTime()) count++;
}
return count;
}
}
54. public String table() {
StringBuilder html = new StringBuilder();
html.append("<table>");
for (Employee employee : employees) {
html.append("<tr>");
// Print employee data ...
html.append("</tr>");
}
html.append("<tr>");
html.append("<td colspan='3'>");
html.append(
new FullTimeEmployeeCount(employees).count());
html.append("</td>");
html.append("</tr>");
html.append("</table>");
return html.toString();
}
72. Upcoming Event
“How to Hire and Retain Developers”
by Debbie Madden, EVP
Monday @ 6 PM
General Assembly
http://generalassemb.ly/education/
More upcoming NYC and Boston Cyrus events at
http://www.cyrusinnovation.com/about/events
... there is a sense of &#x201C;Yay, pair programming!&#x201D;\n
&#x201C;TDD sounds great, but it won&#x2019;t work on my project.&#x201D;\n
Story: I&#x2019;m a Survivor\n
\n
I&#x2019;m not saying it will be easy. Many legacy projects are in maintenance mode. And maintenance is generally something that doesn&#x2019;t feel particularly exciting or associated with new technology.\n
But I think this may be a general part of the human condition. \nOr at least Kurt Vonnegut did. \n\n\n
In maintenance it can feel as if the all the fun stuff has ended and the rest of the work is trivial. To get in a mindset where working with and testing legacy code can be fun,\n
... I offer you this quote from Michael Feathers \n&#x201C;Design is a continuous process, even when a project is in maintenance mode.&#x201D;\n\n
CALL TO ACTION - If you are a programmer and you haven&#x2019;t read this book, read it!\nDescribes:\n1. How to figure out what legacy code is doing\n2. How to add tests to legacy code bases\n3. Includes a catalog of patterns to make code easier to test\n
\n
This is the TDD Cycle. But on legacy code, even the first step of &#x201C;Getting to Red&#x201D; can be unpredictably hard.\nAnd so to get in to this loop, we need to figure out how to test our code -- and why it&#x2019;s hard to test it.\n
\n
Highly coupled code is difficult to test because it can be hard to get small units to operate in isolation, and it can be hard to observe the effect you&#x2019;re trying to test for.\n\nTesting Legacy Code is all about improving and refactoring the design.\n\nThat usually means decoupling highly-coupled code, and making big structures smaller.\n
= Easier to reason about and test\n
So how do you add tests to code that wasn&#x2019;t designed for testing?\n
&#x201C;A seam is a place where you can alter behavior in your program without editing in that place.&#x201D; - Feathers\n\nWhy Seams? They are places where it becomes possible to replace undesirable production behavior or observe changes.\n
Sometimes the seams are there already, so you can just use them.\nBut if there isn&#x2019;t a seam, you may need to change your code to create one.\n\n
But how do you change the code if you&#x2019;re supposed to have a test for it in the first place?\n
The initial result may look weird, and maybe even ugly, but over time these tend to get smoothed over.\n\nAnd how do you break these dependencies safely?\n
\n
Concentrate on making the minimal amount of changes to get you into a place where you can test.\nAnd personally, I find that.... (finding seams can be fun!)\n
Finding seams can be fun!\n\n\n
! Language Disclaimer !\n
\n
\n
\n
\n
\n
\n
If you want, you can leave the single argument constructor from before and have it delegate to the new one, so that production code is left unchanged.\n
Do whatever it takes!\n
\n
The &#x2018;sent&#x2019; byte array is public, which we&#x2019;d probably never do in production code, but it till get us going for our tests.\nAnd we may not know what &#x2018;sendBytes&#x2019; needs to return, so we just send\n
\n
\n
\n
\n
\n
\n\n
... and you don&#x2019;t want to break the habit of writing a test first and then production code.\n
\n
\n
\n
\n
\n
\n
\n
Tend to prevent more regressions in the project at first\nEasier to sell to new developers\nBut massive amounts can be expensive\n... Generally a few catch many deployment-related issues.\nStory: It&#x2019;s all Selenium\n
\n
1. New People (to TDD. and maybe the code)\n2. Legacy Code\n3. Political\n
\n
\n
\n
Being persistent\n
- bugs \n- new features\n
\n
\n
Pairing can help\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
I&#x2019;m not saying it will be easy. Many legacy projects are in maintenance mode. And maintenance is generally something that doesn&#x2019;t feel particularly exciting or associated with new technology.\n
\n
Many techniques are available for this\nDebugging\nRefactoring for learning, then reverting\nCharacterization Tests\n