Identify responsibilities Does the class have a single responsibility? Yes test drive behaviour as normal No identify collaborating roles (interfaces) specify interactions using test doubles
queries; mock commands Doubles can return value objects or other doubles (but not objects with behaviour) Don’t stub or mock value objects or primitives
queries; mock commands Doubles can return value objects or other doubles (but not objects with behaviour) Don’t stub or mock value objects or primitives Don’t stub or mock the object under test
queries; mock commands Doubles can return value objects or other doubles (but not objects with behaviour) Don’t stub or mock value objects or primitives Don’t stub or mock the object under test
queries; mock commands Doubles can return value objects or other doubles (but not objects with behaviour) Don’t stub or mock value objects or primitives Don’t stub or mock the object under test *more what you’d call ‘guidelines’ than actual rules
public void authenticate(String username, Session session) { } } public interface Repository { User findUser(String username); } public interface Session { void setUser(User user); } public class User { }
just turned on$") public void a_calculator_I_just_turned_on() { calc = new Calculator(); } @When("^I add (\\d+) and (\\d+)$") public void adding(int arg1, int arg2) { calc.push(arg1); calc.push("+"); calc.push(arg2); } @Then("^the result is (\\d+)$") public void the_result_is(double expected) { assertEquals(expected, calc.value()); } }
just turned on$") public void a_calculator_I_just_turned_on() { calc = new Calculator(); } @When("^I add (\\d+) and (\\d+)$") public void adding(int arg1, int arg2) { calc.push(arg1); calc.push("+"); calc.push(arg2); } @Then("^the result is (\\d+)$") public void the_result_is(double expected) { assertEquals(expected, calc.value()); } }
just turned on$") public void a_calculator_I_just_turned_on() { calc = new Calculator(); } @When("^I add (\\d+) and (\\d+)$") public void adding(int arg1, int arg2) { calc.push(arg1); calc.push("+"); calc.push(arg2); } @Then("^the result is (\\d+)$") public void the_result_is(double expected) { assertEquals(expected, calc.value()); } }
just turned on$") public void a_calculator_I_just_turned_on() { calc = new Calculator(); } @When("^I add (\\d+) and (\\d+)$") public void adding(int arg1, int arg2) { calc.push(arg1); calc.push("+"); calc.push(arg2); } @Then("^the result is (\\d+)$") public void the_result_is(double expected) { assertEquals(expected, calc.value()); } }
just turned on$") public void a_calculator_I_just_turned_on() { calc = new Calculator(); } @When("^I add (\\d+) and (\\d+)$") public void adding(int arg1, int arg2) { calc.push(arg1); calc.push("+"); calc.push(arg2); } @Then("^the result is (\\d+)$") public void the_result_is(double expected) { assertEquals(expected, calc.value()); } }
just turned on$") public void a_calculator_I_just_turned_on() { calc = new Calculator(); } @When("^I add (\\d+) and (\\d+)$") public void adding(int arg1, int arg2) { calc.push(arg1); calc.push("+"); calc.push(arg2); } @Then("^the result is (\\d+)$") public void the_result_is(double expected) { assertEquals(expected, calc.value()); } }
Given it is 17.30 on 25/10/2014 When I want to travel from Ipswich to Liverpool Street And I want to travel on 27/10/2014 And I search for trains Then the first train should leave at 17:43 public class TrainTimesStepdefs { @When("^I want to travel from (.+) to (.+)$”) public void chooseStations(String from, String to) { WebDriver driver = new FirefoxDriver(); driver.get(“http://www.nationalrail.co.uk/"); element = driver.findElement(By.name("txtFrom")); element.sendKeys(from); element = driver.findElement(By.name("txtTo")); element.sendKeys(to); } }
Given it is 17.30 on 25/10/2014 When I want to travel from Ipswich to Liverpool Street And I want to travel on 27/10/2014 And I search for trains Then the first train should leave at 17:43 public class TrainTimesStepdefs { @When("^I want to travel from (.+) to (.+)$") public void chooseStations(String from, String to) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.nationalrail.co.uk/"); element = driver.findElement(By.name("txtFrom")); element.sendKeys(from); element = driver.findElement(By.name("txtTo")); element.sendKeys(to); } }
Given it is 17.30 on 25/10/2014 When I want to travel from Ipswich to Liverpool Street And I want to travel on 27/10/2014 And I search for trains Then the first train should leave at 17:43 public class TrainTimesStepdefs { @When("^I want to travel from (.+) to (.+)$") public void chooseStations(String from, String to) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.nationalrail.co.uk/"); element = driver.findElement(By.name("txtFrom")); element.sendKeys(from); element = driver.findElement(By.name("txtTo")); element.sendKeys(to); } }
Given it is 17.30 on 25/10/2014 When I want to travel from Ipswich to Liverpool Street And I want to travel on 27/10/2014 And I search for trains Then the first train should leave at 17:43 public class TrainTimesStepdefs { @When("^I want to travel from (.+) to (.+)$") public void chooseStations(String from, String to) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.nationalrail.co.uk/"); element = driver.findElement(By.name("txtFrom")); element.sendKeys(from); element = driver.findElement(By.name("txtTo")); element.sendKeys(to); } }