The Beauty of TDD
How you can use Test Driven Development to make things better
TDD
Red Green Refactor
Why TDD?
Pencil Durability - A Kata
As a writer
I want to be able use a pencil to write text on a sheet of paper
so that I can better remember my thoughts
When the pencil is instructed to write a string of text on a sheet of paper, the paper should reflect the text that was written.
Text written by the pencil should always be appended to existing text on the paper. Thus, given a piece of paper with the text "She sells sea shells", when a pencil is instructed to write " down by the sea shore" on the paper, the paper will then contain the entire string (i.e. "She sells sea shells down by the sea shore").
Paper Returns Text
@Test
public void paperReturnsText() {
Pencil pencil = new Pencil();
String input = "She sells sea shells";
Paper paper = new Paper();
pencil.write(input, paper);
Assert.assertEquals(input, paper.getPage());
}
Paper Returns Text
public class Pencil {
public void write(String text, Paper paper) {
}
}
Paper Returns Text
public class Paper {
public String getPage() {
return "She sells sea shells";
}
}
The smallest step
The smallest step
The Smallest STep
What did we test?
What do we test next?
Paper Returns Text
@Test
public void paperReturnsText() {
Pencil pencil = new Pencil();
String input = "She sells sea shells";
Paper paper = new Paper();
pencil.write(input, paper);
Assert.assertEquals(input, paper.getPage());
}
Pencil Writes Text
@Test
public void pencilWritesTextToPaper() {
Pencil pencil = new Pencil();
String input = "Down by the sea shore";
Paper paper = new Paper();
pencil.write(input, paper);
Assert.assertEquals(input, paper.getPage());
}
Pencil Writes Text
public class Paper {
private String text = "";
public String getPage() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
Pencil Writes Text
public class Pencil {
public void write(String text, Paper paper) {
paper.setText(text);
}
}
What did we test?
Pencil Durability - A Kata
As a writer
I want to be able use a pencil to write text on a sheet of paper
so that I can better remember my thoughts
When the pencil is instructed to write a string of text on a sheet of paper, the paper should reflect the text that was written.
Text written by the pencil should always be appended to existing text on the paper. Thus, given a piece of paper with the text "She sells sea shells", when a pencil is instructed to write " down by the sea shore" on the paper, the paper will then contain the entire string (i.e. "She sells sea shells down by the sea shore").
Pencil Appends Text
@Test
public void pencilAddsTextToPaper() {
Pencil pencil = new Pencil();
String first = "first part";
String second = "second part";
Paper paper = new Paper();
pencil.write(first, paper);
pencil.write(second, paper);
Assert.assertEquals(first+second, paper.getPage());
}
Pencil Writes Text
public class Pencil {
public void write(String text, Paper paper) {
paper.setText(text);
}
}
Pencil Appends Text
public class Paper {
private String text = "";
public String getPage() {
return text;
}
public void setText(String text) {
this.text += text;
}
}
Pencil Appends Text
public class Paper {
private String text = "";
public String getPage() {
return text;
}
public void setText(String text) {
this.text += text;
}
}
What did we test?
Refactoring with Tests
public class Paper {
private String text = "";
public String getPage() {
return text;
}
public void addText(String text) {
this.text += text;
}
}
Refactoring with Tests
public class Pencil {
public void write(String text, Paper paper) {
paper.addText(text);
}
}
Pencil Durability - A Kata
As a writer
I want to be able use a pencil to write text on a sheet of paper
so that I can better remember my thoughts
When the pencil is instructed to write a string of text on a sheet of paper, the paper should reflect the text that was written.
Text written by the pencil should always be appended to existing text on the paper. Thus, given a piece of paper with the text "She sells sea shells", when a pencil is instructed to write " down by the sea shore" on the paper, the paper will then contain the entire string (i.e. "She sells sea shells down by the sea shore").
REfactoring the Tests Themselves
Refactoring the Tests
@Test
public void paperReturnsText() {
Pencil pencil = new Pencil();
String input = "She sell sea shells";
Paper paper = new Paper();
pencil.write(input, paper);
Assert.assertEquals(input, paper.getPage());
}
@Test
public void pencilWritesTextToPaper() {
Pencil pencil = new Pencil();
String input = "Down by the sea shore";
Paper paper = new Paper();
pencil.write(input, paper);
Assert.assertEquals(input, paper.getPage());
}
@Test
public void pencilAddsTextToPaper() {
Pencil pencil = new Pencil();
String first = "first part";
String second = "second part";
Paper paper = new Paper();
pencil.write(first, paper);
pencil.write(second, paper);
Assert.assertEquals(first+second, paper.getPage());
}
Refactoring the Tests
public class WriteOnPaperTest {
private Pencil pencil;
private Paper paper;
@Before
public void setup(){
pencil = new Pencil();
paper = new Paper();
}
Refactoring the Tests
@Test
public void paperReturnsText() {
Pencil pencil = new Pencil();
String input = "She sell sea shells";
Paper paper = new Paper();
pencil.write(input, paper);
Assert.assertEquals(input, paper.getPage());
}
Refactoring the Tests
@Test
public void paperReturnsText() {
Pencil pencil = new Pencil();
String input = "She sell sea shells";
Paper paper = new Paper();
pencil.write(input, paper);
Assert.assertEquals(input, paper.getPage());
}
Refactoring the Tests
@Test
public void paperReturnsText() {
String input = "She sell sea shells";
pencil.write(input, paper);
Assert.assertEquals(input, paper.getPage());
}
The Focus of TDD
Real Life
Cheating
Level of detail
Review
Try the Kata yourself!
Feedback