290 likes | 302 Views
http://flic.kr/p/bLeoRk. Test-Driven Development. From a testing perspective, why might this scenario end badly?. Initial Plan: Implement software, then test at end Implementation takes 6 months longer than expected Finally, it ’ s time to test. System is huge, so testing job is HUGE
E N D
http://flic.kr/p/bLeoRk Test-Driven Development
From a testing perspective, why might this scenario end badly? • Initial Plan: Implement software, then test at end • Implementation takes 6 months longer than expected • Finally, it’s time to test • System is huge, so testing job is HUGE • Pressure to ship causes skimping on testing • Defects discovered this late are costly to fix • But how to test earlier?
Test-Driven Development (TDD) • Idea: Test First! • Write unit tests before functional code • Typically blackbox tests
Iterative Development Process Analysis Requirements Design InitialPlanning Implementation Planning TDD combines Testing Evaluation Deployment
US and Tasks Start withTask 1
Further Break Down the Task • Represent order info • Represent gift card info • Represent receipt info *Although these might have been tasks, they’re very small, so it made more sense to combine them into a larger task
Rule #1: Your test should fail before you implement any code • Establishes a measure of success • Promotes programming incrementally
Rule #2: Implement the simplest code possible to make the test pass • Also helps to promote incremental programming • By focusing on small bits of code • Helps resist urge to add unwanted extras
In writing the test, you design the class interface — just enough interface!
Next (Green): Implement the interface you designed for the test
If in the process of building up classes, you realize your design could be improved, thenREFACTOR!… and continue going around and around …
As you go, you expand upon the systems capabilities • You might do a test for each of these cases: • A gift card with more than enough to cover the cost of the order • A gift card without enough to cover the cost of the order An invalid gift card number • A gift card with exactly the right amount • A gift card that hasn’t been activated • A gift card that’s expired
Pros/Cons of TDD • Pros: • Yields lots of test cases • More tests leads to increased confidence • Cons: • False sense of confidence? • Non-TDD folks may not understand why writing so many tests and not functionality Despite cons, TDD is a widely advocated practice
Problem: How do you write a unit test for this? DB-type I/O makes unit tests slow and complex
Here’s what we need for the system Actual DB I/O
But here’s what we need for unit testing Simulated (“fake”) DB I/O
How might you make the actual and the test DB utilities interchangeable?
Strategy Pattern Is One Way OrderProcessor uses DBAccessor interface instead of either concrete class
Another Way: Mocks • Stand-ins for real objects • Especially good if faced with creating lots of concrete strategies • Requires framework (similar to JUnit)
Mocks Example RecordMode This may seem like a lot of code, but compared to a bunch of classes…
Practical Tips on Mocking • Several frameworks • Mockito actually seems to be most popular • URL: http://mockito.org/ • Maven dependency: org.mockito mockito-all • Servlets can be unit (not integration) tested using mocks • Requires a lot of mocks (request, response, session, etc.) • Examples: • http://johannesbrodwall.com/2009/10/24/testing-servlets-with-mockito/ • http://stackoverflow.com/questions/5434419/how-to-test-my-servlet-using-junit
What’s next? • Productivity report due in one week (Tue) • 1.5 days of work per team member • Exam 2 on next Thu