You are previewing Test-Driven Java Development.
O'Reilly logo
Test-Driven Java Development

Book Description

Invoke TDD principles for end-to-end application development with Java

About This Book

  • Explore the most popular TDD tools and frameworks and become more proficient in building applications

  • Create applications with better code design, fewer bugs, and higher test coverage, enabling you to get them to market quickly

  • Implement test-driven programming methods into your development workflows

  • Who This Book Is For

    If you’re an experienced Java developer and want to implement more effective methods of programming systems and applications, then this book is for you.

    What You Will Learn

  • Explore the tools and frameworks required for effective TDD development

  • Perform the Red-Green-Refactor process efficiently, the pillar around which all other TDD procedures are based

  • Master effective unit testing in isolation from the rest of your code

  • Design simple and easily maintainable codes by implementing different techniques

  • Use mocking frameworks and techniques to easily write and quickly execute tests

  • Develop an application to implement behaviour-driven development in conjunction with unit testing

  • Enable and disable features using Feature Toggles

  • In Detail

    Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasises writing a test before writing the necessary code, and then refactoring the code to optimize it.

    The value of performing TDD with Java, one of the most established programming languages, is to improve the productivity of programmers, the maintainability and performance of code, and develop a deeper understanding of the language and how to employ it effectively.

    Starting with the basics of TDD and reasons why its adoption is beneficial, this book will take you from the first steps of TDD with Java until you are confident enough to embrace the practice in your day-to-day routine.

    You’ll be guided through setting up tools, frameworks, and the environment you need, and will dive right in to hands-on exercises with the goal of mastering one practice, tool, or framework at a time. You’ll learn about the Red-Green-Refactor procedure, how to write unit tests, and how to use them as executable documentation.

    With this book you’ll also discover how to design simple and easily maintainable code, work with mocks, utilise behaviour-driven development, refactor old legacy code, and release a half-finished feature to production with feature toggles.

    You will finish this book with a deep understanding of the test-driven development methodology and the confidence to apply it to application programming with Java.

    Style and approach

    An easy-to-follow, hands-on guide to building applications through effective coding practices. This book covers practical examples by introducing different problems, each one designed as a learning exercise to help you understand each aspect of TDD.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Test-Driven Java Development
      1. Table of Contents
      2. Test-Driven Java Development
      3. Credits
      4. About the Authors
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Errata
          2. Piracy
          3. Questions
      8. 1. Why Should I Care for Test-driven Development?
        1. Why TDD?
          1. Understanding TDD
          2. Red-green-refactor
          3. Speed is the key
          4. It's not about testing
        2. Testing
          1. The black-box testing
          2. The white-box testing
          3. The difference between quality checking and quality assurance
          4. Better tests
        3. Mocking
        4. Executable documentation
        5. No debugging
        6. Summary
      9. 2. Tools, Frameworks, and Environments
        1. Git
        2. Virtual machines
          1. Vagrant
          2. Docker
        3. Build tools
        4. The integrated development environment
          1. The IDEA demo project
        5. Unit testing frameworks
          1. JUnit
          2. TestNG
        6. Hamcrest and AssertJ
          1. Hamcrest
          2. AssertJ
        7. Code coverage tools
          1. JaCoCo
        8. Mocking frameworks
          1. Mockito
          2. EasyMock
          3. Extra power for mocks
        9. User interface testing
          1. Web testing frameworks
          2. Selenium
          3. Selenide
        10. The behavior-driven development
          1. JBehave
          2. Cucumber
        11. Summary
      10. 3. Red-Green-Refactor – from Failure through Success until Perfection
        1. Setting up the environment with Gradle and JUnit
          1. Setting up Gradle/Java project in IntelliJ IDEA
        2. The red-green-refactor process
          1. Write a test
          2. Run all the tests and confirm that the last one is failing
          3. Write the implementation code
          4. Run all the tests
          5. Refactor
          6. Repeat
        3. The Tic-Tac-Toe game requirements
        4. Developing Tic-Tac-Toe
          1. Requirement 1
            1. Test
            2. Implementation
            3. Test
            4. Implementation
            5. Test
            6. Implementation
            7. Refactoring
          2. Requirement 2
            1. Test
            2. Implementation
            3. Test
            4. Implementation
            5. Test
          3. Requirement 3
            1. Test
            2. Implementation
            3. Test
            4. Implementation
            5. Refactoring
            6. Test
            7. Implementation
            8. Test
            9. Implementation
            10. Test
            11. Implementation
            12. Refactoring
          4. Requirement 4
            1. Test
            2. Implementation
            3. Refactoring
        5. Code coverage
        6. More exercises
        7. Summary
      11. 4. Unit Testing – Focusing on What You Do and Not on What Has Been Done
        1. Unit testing
          1. What is unit testing?
          2. Why unit testing?
          3. Code refactoring
          4. Why not use unit tests exclusively?
        2. Unit testing with TDD
        3. TestNG
          1. The @Test annotation
          2. The @BeforeSuite, @BeforeTest, @BeforeGroups, @AfterGroups, @AfterTest, and @AfterSuite annotations
          3. The @BeforeClass and @AfterClass annotations
          4. The @BeforeMethod and @AfterMethod annotations
          5. The @Test(enable = false) annotation argument
          6. The @Test(expectedExceptions = SomeClass.class) annotation argument
          7. TestNG vs JUnit summary
        4. Remote controlled ship requirements
        5. Developing the remote-controlled ship
          1. Project setup
          2. Helper classes
          3. Requirement 1
            1. Specification
            2. Specification implementation
            3. Refactoring
          4. Requirement 2
            1. Specification
            2. Specification implementation
            3. Specification
            4. Specification implementation
          5. Requirement 3
            1. Specification
            2. Specification implementation
            3. Specification
            4. Specification implementation
          6. Requirement 4
            1. Specification
            2. Specification implementation
            3. Specification
            4. Specification implementation
          7. Requirement 5
            1. Specification
            2. Specification implementation
            3. Refactoring
            4. Specification
            5. Specification implementation
          8. Requirement 6
        6. Summary
      12. 5. Design – If It's Not Testable, It's Not Designed Well
        1. Why should we care about design?
          1. Design principles
            1. You Ain't Gonna Need It
            2. Don't Repeat Yourself
            3. Keep It Simple, Stupid
            4. Occam's Razor
            5. SOLID
        2. Connect4
          1. Requirements
        3. Test the last implementation of Connect4
          1. Requirement 1
          2. Requirement 2
          3. Requirement 3
          4. Requirement 4
          5. Requirement 5
          6. Requirement 6
          7. Requirement 7
          8. Requirement 8
        4. The TDD implementation of Connect4
          1. Hamcrest
          2. Requirement 1
            1. Tests
            2. Code
          3. Requirement 2
            1. Tests
            2. Code
          4. Requirement 3
            1. Tests
            2. Code
          5. Requirement 4
            1. Tests
            2. Code
          6. Requirement 5
            1. Tests
            2. Code
          7. Requirement 6
            1. Tests
            2. Code
          8. Requirement 7
            1. Tests
            2. Code
          9. Requirement 8
            1. Tests
            2. Code
        5. Summary
      13. 6. Mocking – Removing External Dependencies
        1. Mocking
          1. Why mocks?
          2. Terminology
          3. Mock objects
        2. Mockito
        3. The Tic-Tac-Toe v2 requirements
        4. Developing Tic-Tac-Toe v2
          1. Requirement 1
            1. Specification and specification implementation
            2. Specification
            3. Specification implementation
            4. Specification
            5. Implementation
            6. Refactoring
            7. Specification
            8. Specification implementation
            9. Specification
            10. Specification implementation
            11. Refactoring
            12. Specification
            13. Specification implementation
            14. Specification
            15. Specification implementation
            16. Specification
            17. Specification implementation
            18. Specification
            19. Specification implementation
          2. Requirement 2
            1. Specification
            2. Specification implementation
            3. Specification refactoring
            4. Specification
            5. Specification implementation
            6. Specification
            7. Specification implementation
            8. Specification
            9. Specification implementation
            10. Exercises
        5. Integration tests
          1. Tests separation
          2. The integration test
        6. Summary
      14. 7. BDD – Working Together with the Whole Team
        1. Different specifications
          1. Documentation
          2. Documentation for coders
          3. Documentation for non-coders
        2. Behavior-driven development
          1. Narrative
          2. Scenarios
        3. The Books Store BDD story
        4. JBehave
          1. JBehave runner
          2. Pending steps
          3. Selenium and Selenide
          4. JBehave steps
          5. Final validation
        5. Summary
      15. 8. Refactoring Legacy Code – Making it Young Again
        1. Legacy code
          1. Legacy code example
            1. Other ways to recognize legacy code
            2. A lack of dependency injection
            3. The legacy code change algorithm
            4. Applying the legacy code change algorithm
              1. Identifying change points
              2. Finding test points
              3. Breaking dependencies
              4. Writing tests
        2. The Kata exercise
          1. Legacy Kata
          2. Description
          3. Technical comments
          4. Adding a new feature
          5. Black-box or spike testing
          6. Preliminary investigation
            1. How to find candidates for refactoring
            2. Introducing the new feature
          7. Applying the legacy code algorithm
            1. Writing end-to-end test cases
            2. Automating the test cases
            3. Injecting the BookRepository dependency
          8. Extract and override call
            1. Parameterizing a constructor
            2. Adding a new feature
          9. Removing the primitive obsession with status as Int
        3. Summary
      16. 9. Feature Toggles – Deploying Partially Done Features to Production
        1. Continuous Integration, Delivery, and Deployment
        2. Feature Toggles
        3. A Feature Toggle example
          1. Implementing the Fibonacci service
          2. Working with the template engine
        4. Summary
      17. 10. Putting It All Together
        1. TDD in a nutshell
        2. Best practices
          1. Naming conventions
          2. Processes
          3. Development practices
          4. Tools
        3. This is just the beginning
        4. This does not have to be the end
      18. Index