we get to a known state with the new functionality. You can commit any number of sins to get there, because speed trumps design, just for that brief moment. To quote Kent Beck...
2. Locate highest layer @ which to make a change (entrypoint). 3. Write a test, optionally Mocking significant collaborators. Get them to pass. 4. Write tests for collaborators, make them pass. 5. Repeat 4 for collaborator’s collaborators. 6. Run high level test. If it passes, you’re good.
much going on!!! Extract collaborating object. 1. Move implementation over to new class. 2. Run existing tests. If Green, great! 3. Copy behavior specific tests to collaborator unit tests. Green? Great! 4. Delete tests in original object and Mock interaction (optional/per best judgment)
your friend… Break hard dependencies. function ShoppingController(){ this.on("theyWannaCheckoutEvent", function(){ window.PaymentGateway.chargeCustomer(50.50); }) } describe(“A test that really charges the customer”, function(){ it(“Drains David’s Bank Account”, function(){ new ShoppingController().trigger(“theyWann aCheckoutEvent”); //Uhhhh expect me to check my balance //in the Charles Schwab app... } }
@RequestMapping(value ="/users/{userId}", method = RequestMethod.GET) public Account getAccount(@PathVariable Long userId){return null;} } @Test public void getAccount() throws Exception { when(userService.findUser(anyInt())).thenReturn("element"); this.mockMvc.perform(get("/users/123") andExpect(status().isOk()); } @Test public void getAccount() throws Exception { when(userService.findUser(anyInt())).thenReturn("element"); this.mockMvc.perform(get("/users/123") andExpect(status().isOk()); verify(userService.findUser(123); } @RestController class UserController{ @Autowired public UserController(UserService usvc){...} @RequestMapping(value ="/users/{userId}", method = RequestMethod.GET) public Account getAccount(@PathVariable Long userId){ userService.findUser(123) return null; }} Test that we correctly interact with the framework