This course provides everything you need to know to get started with test driven development in Java.
NEW! Discount codes for the online course!
If you like the look of these slides and would like to take the online course on Udemy, use discount code TDDSLIDES for a massive discount - https://www.udemy.com/learn-test-driven-development-in-java/?couponCode=TDDSLIDES
This course covers test driven development from scratch, through video lectures, demonstrations of practising a test driven approach, and through exercises for you to complete, allowing you to gain valuable experience in using TDD.
For the very best in interactive courses for software developers see http://www.uncademy.io
2. About me!
Matthew Todd matthew.todd@fluentsoftware.co.uk
@matthew_todd
Working with developers,
continuously improving agile practice
Wednesday, 14 November 12
4. Testing timeline
1994: Kent Beck creates SUnit test framework
1999: Extreme programming explained published
1989: First tests on punchcards 1995: TDD Demo 2000: JUnit.org launched
Wednesday, 14 November 12
5. Manifesto for Agile Software Development
We are uncovering better ways of developing
software by doing it and helping others do it.
Through this work we have come to value:
Individuals and interactions over processes and tool s
Working software over comprehensive documentation
Customer collaboration over contract negotiation
Responding to change over following a plan
That is, while there is value in the items on
the right, we value the items on the left more
Wednesday, 14 November 12
7. Test spectrum Full system test
Automated integration/acceptance
Testing in concert
Component test
Unit test
Single function test
Wednesday, 14 November 12
15. Proactive VS Reactive
Seek out problems early on Test late
Validate requirements Only respond to defects
Validate code Focused on debugging
Wednesday, 14 November 12
16. Relative cost of change
Cost
Unit test Live system
Time
Wednesday, 14 November 12
17. Requirements
Drive out requirements issues early
Wednesday, 14 November 12
18. Rapid feedback
Many small changes VS One Big Change™
Wednesday, 14 November 12
19. Collaboration
Enables developers to work together
Wednesday, 14 November 12
20. Values refactoring
Refactor often to lower impact and risk
Wednesday, 14 November 12
21. Design to test
Testing driving good design practice
Wednesday, 14 November 12
22. Tests as information
Documenting decisions and assumptions
Wednesday, 14 November 12
72. Anti-pattern: the singleton
Wednesday, 14 November 12
73. Anti-pattern: the singleton
Who uses this singleton?
Who owns this singleton?
What behaviours does it provide?
Wednesday, 14 November 12
74. Anti-pattern: create the world
@Before
public void initialiseTests() {
loadBalancer = createMockLoadBalancingService();
server1 = createMockServer();
server2 = createMockServer();
loadBalancer.addServer(server1);
loadBalancer.addServer(server2);
dbEngine = new DBEngine();
dbEngine.addTable("users");
dbEngine.addTable("profiles");
dbEngine.addTable("posts");
userProfileService = new Mock<UserProfileServer>();
adminUser = new User("admin",true);
user1 = new User("alice",false);
user2 = new User("bob",false);
userProfileService.register(user1);
userProfileService.register(user2);
user1.connectWith(user2);
...
Wednesday, 14 November 12
75. Anti-pattern: create the world
Test burden increases over time
Test complexity increases
Often a sign of application complexity
Wednesday, 14 November 12
76. Anti-pattern: completely mocked
@Test
public void testingNothing() {
MockServiceFactory factory = new MockServiceFactory();
Mock<UserService> userService = new Mock<UserService>();
factory.add(userService);
User user = new User("bob");
userService.add(user);
assertEquals("bob",userService.getUsers()[0].name());
}
Wednesday, 14 November 12
77. Test boundaries
Class(es) under test Out of test scope
Wednesday, 14 November 12
78. Anti-pattern: exceptional test
@Test
public void testUserCreation() {
userService.createUser("bob");
}
Wednesday, 14 November 12
79. Anti-pattern: exceptional test
@Test
public void testUserCreation() {
userService.createUser("bob");
}
Good coverage,
but poor validation
Wednesday, 14 November 12
80. Anti-pattern: usually passes
com.example.UncertainTest
com.example.UncertainTest
com.example.UncertainTest
com.example.UncertainTest
com.example.UncertainTest
Wednesday, 14 November 12
81. Anti-pattern: usually passes
Control threading and access
to resources
We need to be able to trust our tests
Wednesday, 14 November 12
82. Anti-pattern: one big test
@Test
public void testEverything() {
userService.createUser("alice");
assertEquals(1,userService.getUserCount());
userService.createUser("alice");
assertEquals(1,userService.getUserCount());
userService.createUser("bob");
assertEquals(2,userService.getUserCount());
userService.dropUser("alice");
assertEquals(1,userService.getUserCount());
User bob = userService.getUser("bob");
assertNotNull(bob);
...
Wednesday, 14 November 12
83. Anti-pattern: one big test
testUserCreation
testEverything testCreatingDuplicateUser
testDroppingUser
Wednesday, 14 November 12
84. Anti-pattern: the slow test
Wednesday, 14 November 12
85. Anti-pattern: the slow test
Should be quick to pass
Should be quick to fail
Run frequently
Wednesday, 14 November 12
86. Anti-pattern: second-class test
@Test
public void whoKnowsWhatThisTests() {
createTestObjects();
testUser.setName("alice");
setupTestDependencies();
User user2 = new User("user2");
setupUser(user2);
testManager.add(testUser);
...
test(user2);
}
Wednesday, 14 November 12
92. TDD Kata roman numerals
1 - convert text from normal numerals ->
Roman numerals
2 - convert in the other direction too
Wednesday, 14 November 12
93. TDD Kata FizzBuzz
printing numbers from 1 to 100
..but multiples of 3 should print Fizz
multiples of 5 should print Buzz
multiples of 3 and 5 print FizzBuzz
1 2 Fizz 4 Buzz Fizz 7 ...
Wednesday, 14 November 12
94. TDD Kata roman calculator
calculator only accepting roman numerals as
input and output
I + III = IV
Wednesday, 14 November 12
95. TDD Kata tennis
implement simple tennis game
http://en.wikipedia.org/wiki/Tennis#Scoring
Wednesday, 14 November 12