19. TDD Lifecycle - Detailed
!
Add a Test – Each new feature begins with writing a test. This test must
initially fail since it is written before the feature has been coded.!
!
Run All Tests – validate that the test-harnesses work and that the new
test does not mistakenly pass without requiring any new code.!
!
Write Development Code – That will cause the test to pass.!
!
Run Tests – If all test cases pass the code meets all the tested
requirements.!
!
Clean Up Code – Refactor code to make production ready removing
any dummy/stub code.!
!
Repeat Process – Starting with another new test, the cycle is then
repeated to push forward the functionality.!
20. TDD Results!
•
Testable Code
•
Working Software
•
Enforce Thinking Ahead
•
Clean & Maintainable Code
•
Increment Code Quality
•
Faster Feedback
•
Bring Back the Joy of Coding
21. TDD in a nutshell!
1. Write a test!
2. Watch the test fail!
3. Write just enough code !
4. Pass the test!
5. Refactor, remove duplications!
6. Pass the test again!
22. TDD in extremely simplified
!
• Write a failing test!
• Pass the test!
• Repeat!
25. Unit Testing
!
A unit test is a piece of code that
invokes a unit of work in the system
and then checks a single
assumption about the behavior of
that unit of work.!
26. Unit of Work
!
• A unit of work is a single logical functional
use case in the system that can be invoked
by some public interface (in most cases)!
• A unit of work can span a single method, a
whole class or multiple classes working
together to achieve one single logical
purpose that can be verified!
29. Assertions!
A confident and forceful statement
of fact or belief.!
!
“I assert that the earth is round”!
!
But if the assertion is wrong, then there is something fundamentally
flawed about the understanding.!
!
This is the fundamental principle used in unit
testing.!
30. Assertions in coding!
• At this point in my code, I assert these two
strings are equal!
!
• At this point in my code, I assert this object
is not null!
31. Assert in JUnit!
assertEquals("failure - strings not same", 5l, 5l);
assertNotNull("should not be null", new Object());
int objA[] = new int[10];
int objB[] = new int[10];
assertArrayEquals(objA, objB);
https://github.com/junit-team/junit/wiki/Assertions
33. Bank Account!
Sprint 1!
!
•
•
•
•
Create a bank account!
Deposit 100!
Withdraw 50!
Check the balance!
!
!
Sprint 2
!
• Max withdrawal is 5000!
• Add penalty of 5, if withdraw more than the balance!
!
!
Sprint 3
• Throw an exception if negative value is passed to the deposit method
39. Refactoring!
Refactoring is the process of clarifying and
simplifying the design of existing code,
without changing its behavior.!
!
!
At the smallest scale, a “refactoring” is a
minimal, safe transformation of your code that
keeps its behavior unchanged.!
41. Inline Temporary Variable
!
Replace all uses of a local variable by inserting copies of its assigned value.!
public double applyDiscount() {
double basePrice = _cart.totalPrice();
return basePrice * discount();
}
public double applyDiscount() {
return _cart.totalPrice() * discount();
}
42. Extract method!
Create a new method by extracting a code fragment from an existing!
method.!
public void report(Writer out, List<Machine> machines) throws IOException {
for (Machine machine : machines) {
out.write(“Machine “ + machine.name());
if (machine.bin() != null)
out.write(“ bin=” + machine.bin());
out.write(“n”);
}
}
public void report(Writer out, List<Machine> machines) throws IOException {
for (Machine machine : machines) {
reportMachine(out, machine);
}
}
private void reportMachine(Writer out, Machine machine) throws IOException {
out.write(“Machine “ + machine.name());
if (machine.bin() != null){
out.write(“ bin=” + machine.bin());
}
out.write(“n”);
}
43. String Calculator Exercise!
1. Create a simple String calculator with a method int Add(string
numbers)!
1. The method can take 0, 1 or 2 numbers, and will return their sum
(for an empty string it will return 0) for example “” or “1” or “1,2”!
2. Start with the simplest test case of an empty string and move to 1
and two numbers!
3. Remember to solve things as simply as possible so that you force
yourself to write tests you did not think about!
4. Remember to refactor after each passing test!
2. Allow the Add method to handle an unknown amount of numbers!
!
3. Calling Add with a negative number will throw an exception “negatives
not allowed”!