SlideShare ist ein Scribd-Unternehmen logo
1 von 96
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Andere mochten auch

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
 

Andere mochten auch (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
 

Kürzlich hochgeladen

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 

Kürzlich hochgeladen (20)

Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 

Learn Test Driven Development in Java