SlideShare una empresa de Scribd logo
1 de 96
Descargar para leer sin conexión
Test Driven Development
                                               in Java




Wednesday, 14 November 12
About me!

                               Matthew Todd     matthew.todd@fluentsoftware.co.uk
                                                @matthew_todd



                            Working with developers,
                             continuously improving agile practice




Wednesday, 14 November 12
What is TDD?




Wednesday, 14 November 12
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
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
Wednesday, 14 November 12
Test spectrum             Full system test
                                                   Automated integration/acceptance




                             Testing in concert



                                                   Component test


                                      Unit test

                            Single function test




Wednesday, 14 November 12
1. Write test




Wednesday, 14 November 12
1. Write test



                            2. Test fails!




Wednesday, 14 November 12
1. Write test


                               2. Test fails!


                            3. Write code




Wednesday, 14 November 12
1. Write test


                               2. Test fails!


                              3. Write code


                            4. Test passes!


Wednesday, 14 November 12
1. Write test

                             2. Test fails!

                            3. Write code

                            4. Test passes!


                            5. Refactor

Wednesday, 14 November 12
1. Write test

                             2. Test fails!

                            3. Write code

                            4. Test passes!


                             5. Refactor


Wednesday, 14 November 12
Why TDD?




Wednesday, 14 November 12
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
Relative cost of change


                Cost




                            Unit test                             Live system

                                                  Time




Wednesday, 14 November 12
Requirements
                            Drive out requirements issues early




Wednesday, 14 November 12
Rapid feedback
                            Many small changes VS One Big Change™




Wednesday, 14 November 12
Collaboration
                            Enables developers to work together




Wednesday, 14 November 12
Values refactoring
                            Refactor often to lower impact and risk




Wednesday, 14 November 12
Design to test
                            Testing driving good design practice




Wednesday, 14 November 12
Tests as information
                            Documenting decisions and assumptions




Wednesday, 14 November 12
Sounds great!
                                 So show me




Wednesday, 14 November 12
So, let’s get started




Wednesday, 14 November 12
Reverse polish calculator
                                                        34+




Wednesday, 14 November 12
Calculator walkthrough




Wednesday, 14 November 12
Reverse polish calculator
                            Developed using a test driven approach




Wednesday, 14 November 12
“sounds simple,
                              but what about real applications?”




Wednesday, 14 November 12
“sounds simple,
                              but what about real applications?”




Wednesday, 14 November 12
“sounds simple,
                              but what about real applications?”




Wednesday, 14 November 12
SOLID




Wednesday, 14 November 12
Single responsibilityOLID




Wednesday, 14 November 12
Wednesday, 14 November 12
Wednesday, 14 November 12
SOpen/ClosedLID




Wednesday, 14 November 12
Wednesday, 14 November 12
Wednesday, 14 November 12
SOLiskov SubstitutionID




Wednesday, 14 November 12
Wednesday, 14 November 12
SOLInterface segregationD




Wednesday, 14 November 12
Wednesday, 14 November 12
Wednesday, 14 November 12
SOLIDependency inversion




Wednesday, 14 November 12
Wednesday, 14 November 12
Wednesday, 14 November 12
Isolating dependencies

                                                     Dependency

                                   Test target   x
                                                     Test double
                            Test




Wednesday, 14 November 12
Test Doubles
              Stubs             Fakes   Mocks


Wednesday, 14 November 12
Test Doubles
              Stubs             Fakes   Mocks


Wednesday, 14 November 12
Stubs
              Providing state-based verification




Wednesday, 14 November 12
Test Doubles
              Stubs             Fakes   Mocks


Wednesday, 14 November 12
Fakes
                            Providing simplified replacements




Wednesday, 14 November 12
Test Doubles
              Stubs             Fakes   Mocks


Wednesday, 14 November 12
Mocks
                            Providing behaviour based verification




Wednesday, 14 November 12
Stubs            Assertions made
           Fakes            directly in test method


           Mocks            Assertions made by
                            mock object


Wednesday, 14 November 12
Test double demonstration




Wednesday, 14 November 12
Mock frameworks




Wednesday, 14 November 12
Wednesday, 14 November 12
Wednesday, 14 November 12
app ropr iate
                                                     ks w here
                                              amewor
                                    r mock fr
                            Conside        Test balance




Wednesday, 14 November 12
Dealing with the legacy




Wednesday, 14 November 12
Code we cannot change
                                     Dealing with the legacy
                            Someone else’s code?    No interfaces?




Wednesday, 14 November 12
So, now we can test it!




Wednesday, 14 November 12
So, now we can test it!
                                  What makes a good test?




Wednesday, 14 November 12
Test principles




Wednesday, 14 November 12
FIRST




Wednesday, 14 November 12
FastIRST




Wednesday, 14 November 12
FIndependentRST




Wednesday, 14 November 12
FIRepeatableST




Wednesday, 14 November 12
FIRSelf-validatingT




Wednesday, 14 November 12
FIRSTimely




Wednesday, 14 November 12
TDD Anti-patterns




Wednesday, 14 November 12
Anti-pattern:     the singleton




Wednesday, 14 November 12
Anti-pattern:                      the singleton

                               Who uses this singleton?

                                          Who owns this singleton?


                            What behaviours does it provide?



Wednesday, 14 November 12
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
Anti-pattern:                        create the world

                              Test burden increases over time

                                                 Test complexity increases

                            Often a sign of application complexity




Wednesday, 14 November 12
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
Test boundaries
                             Class(es) under test   Out of test scope




Wednesday, 14 November 12
Anti-pattern:                                       exceptional test
                              @Test
                            	  public void testUserCreation() {
                            	  	   userService.createUser("bob");
                            	  }




Wednesday, 14 November 12
Anti-pattern:                                       exceptional test
                              @Test
                            	  public void testUserCreation() {
                            	  	   userService.createUser("bob");
                            	  }




                                               Good coverage,
                                               but poor validation




Wednesday, 14 November 12
Anti-pattern:      usually passes
                            com.example.UncertainTest

                            com.example.UncertainTest

                            com.example.UncertainTest

                            com.example.UncertainTest

                            com.example.UncertainTest



Wednesday, 14 November 12
Anti-pattern:                     usually passes

                            Control threading and access
                            to resources


                            We need to be able to trust our tests




Wednesday, 14 November 12
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
Anti-pattern:                      one big test

                                                testUserCreation


                            testEverything      testCreatingDuplicateUser


                                                testDroppingUser




Wednesday, 14 November 12
Anti-pattern:     the slow test




Wednesday, 14 November 12
Anti-pattern:        the slow test

                            Should be quick to pass


                            Should be quick to fail

                            Run frequently




Wednesday, 14 November 12
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
Applying TDD




Wednesday, 14 November 12
When not to use TDD!




Wednesday, 14 November 12
Continuous delivery

                                                          Integration testing
                                           BDD

                                                    TDD
                                    Design                Manual testing
                            User stories




Wednesday, 14 November 12
Practice practice practice




Wednesday, 14 November 12
TDD Kata




Wednesday, 14 November 12
TDD Kata                            roman numerals
                            1 - convert text from normal numerals ->
                                         Roman numerals
                                   2 - convert in the other direction too




Wednesday, 14 November 12
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
TDD Kata                             roman calculator
                            calculator only accepting roman numerals as
                                          input and output
                                                         I + III = IV




Wednesday, 14 November 12
TDD Kata                       tennis
                            implement simple tennis game
                              http://en.wikipedia.org/wiki/Tennis#Scoring




Wednesday, 14 November 12
Thank you!




Wednesday, 14 November 12

Más contenido relacionado

Destacado

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentConsulthinkspa
 
Java 201 Intro to Test Driven Development in Java
Java 201   Intro to Test Driven Development in JavaJava 201   Intro to Test Driven Development in Java
Java 201 Intro to Test Driven Development in Javaagorolabs
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Zohirul Alam Tiemoon
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)David Ehringer
 

Destacado (6)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Testing In Java
Testing In JavaTesting In Java
Testing In Java
 
Java 201 Intro to Test Driven Development in Java
Java 201   Intro to Test Driven Development in JavaJava 201   Intro to Test Driven Development in Java
Java 201 Intro to Test Driven Development in Java
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 

Último

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Último (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Learn Test Driven Development in Java