Book Image

Test-Driven Java Development, Second Edition - Second Edition

By : Viktor Farcic, Alex Garcia
Book Image

Test-Driven Java Development, Second Edition - Second Edition

By: Viktor Farcic, Alex Garcia

Overview of this book

Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasizes 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 longest established programming languages, is to improve the productivity of programmers and 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 understanding 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 we will dive right into 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, utilize behavior-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.
Table of Contents (18 chapters)
Title Page
Packt Upsell
Contributors
Preface
9
Refactoring Legacy Code – Making It Young Again
Index

Index

A

  • acceptance criteria / Behavior-driven development
  • acceptance test-driven development (ATDD) / The difference between quality checking and quality assurance
  • acceptance tests / Why not use unit tests exclusively?
  • Alexandria project
    • about / Description
    • technical comments / Technical comments
    • new feature, adding / Adding a new feature
    • black-box testing / Black-box or spike testing
    • spike testing / Black-box or spike testing
    • preliminary investigation / Preliminary investigation
    • legacy code algorithm, applying / Applying the legacy code algorithm
    • call, extracting / Extract and override call
    • call, overriding / Extract and override call
    • primitive obsession, removing with status as int / Removing the primitive obsession with status as int
  • application debugging
    • avoiding / No debugging
  • Awesome Gambling Corp
    • about / Case study – Awesome Gambling Corp
    • codebase, exploring / Exploring the codebase
    • release procedure / Release procedure
    • production, deployments / Deployments to production
    • conclusions / Conclusions

B

  • Bamboo
    • reference link / Processes
  • behavior-driven development (BDD)
    • about / Black-box testing, Behavior-driven development
    • specifications / Different specifications
    • narrative / Narrative
    • scenarios / Scenarios
    • book store / The book store BDD story
  • black-box testing
    • about / Black-box testing
    • advantages / Black-box testing
    • disadvantages / Black-box testing
    • reference link / Black-box or spike testing

C

  • call extraction, Alexandria project
    • constructor, parameterizing / Parameterizing a constructor
    • new feature, adding / Adding a new feature
  • call override, Alexandria project
    • constructor, parameterizing / Parameterizing a constructor
    • new feature, adding / Adding a new feature
  • Clover
    • reference link / Tools
  • Cobertura
    • reference link / Tools
  • code coverage
    • reference link / Legacy code example
  • code refactoring / Code refactoring
  • code smell
    • reference link / Legacy code
  • commands, remote-controlled ship development
    • single commands / Specification – single commands, Implementation
    • combined commands / Specification – combined commands, Implementation
  • Connect 4 game
    • about / Connect 4
    • reference link / Connect 4
    • requirements / Requirements
  • constructor chaining / Parameterizing a constructor
  • continuous delivery
    • about / Continuous integration, delivery, and deployment, Towards continuous delivery
    • improvements / Possible improvements
    • test coverage, increasing / Increasing test coverage
    • continuous integration, implementing / Implementing continuous integration
    • Jenkins installation / Jenkins installation
    • builds, automating / Automating builds
    • executing / First execution
    • example / What is next?
  • continuous deployment (CD) / Towards continuous delivery
  • continuous integration (CI) / Executable documentation, Continuous integration, delivery, and deployment, Release procedure
  • curl
    • reference link / Writing end-to-end test cases

D

  • DbUnit
    • reference link / Applying the legacy code algorithm
  • dependency injection / Final considerations, Preliminary investigation
  • Don't Repeat Yourself (DRY) principle
    • reference link / Don't Repeat Yourself

E

  • environment
    • setting up, with Gradle / Setting up the environment with Gradle and JUnit
    • setting up, with JUnit / Setting up the environment with Gradle and JUnit
  • executable documentation / Executable documentation
  • extract / Breaking dependencies
  • Extreme Programming (XP)
    • about / Red-Green-Refactor
    • reference link / Unit testing

F

  • Feature Flags / Feature Toggles
  • Feature Flipping / Feature Toggles
  • Feature Toggles
    • about / Feature Toggles
    • Togglz / Feature Toggles
    • FF4J / Feature Toggles
  • feature toggles, example
    • about / A Feature Toggle example
    • Fibonacci service, implementing / Implementing the Fibonacci service
    • template engine, working with / Working with the template engine
  • FF4J
    • reference link / Feature Toggles
  • Fibonacci's sequence
    • reference link / Implementing the Fibonacci service
  • functional tests / Why not use unit tests exclusively?
  • functions
    • about / Functions revisited
    • Reverse Polish Notation (RPN) / Kata – Reverse Polish Notation

G

  • Gradle project
    • setting up, in IntelliJ IDEA / Setting up Gradle/Java project in IntelliJ IDEA
  • Grizzly
    • reference link / Automating the test cases

H

  • helper classes / Helper classes
  • Hudson
    • reference link / Processes

I

  • infrastructure team (IT) / Deployments to production
  • integration tests / Why not use unit tests exclusively?, Integration tests
  • IntelliJ IDEA
    • Gradle project, setting up / Setting up Gradle/Java project in IntelliJ IDEA
    • Java project, setting up / Setting up Gradle/Java project in IntelliJ IDEA

J

  • Java Code Coverage (JaCoCo)
    • reference link / Tools
  • Java Functional Programming
    • environment, setting up / Setting up the environment
  • Java project
    • setting up, in IntelliJ IDEA / Setting up Gradle/Java project in IntelliJ IDEA
  • JBehave
    • about / JBehave
    • runner / JBehave runner
    • pending steps / Pending steps
    • steps / JBehave steps
    • final validation / Final validation
  • JBehave stories, writing
    • reference link / The book store BDD story
  • Jenkins
    • reference link / Processes
  • JUnit
    • @Test annotation / The @Test annotation
    • @BeforeClass annotation / The @BeforeClass and @AfterClass annotations
    • @BeforeMethod annotation / The @BeforeMethod and @AfterMethod annotations
    • @AfterMethod annotation / The @BeforeMethod and @AfterMethod annotations
    • @Test(expectedExceptions = SomeClass.class) annotation argument / The @Test(expectedExceptions = SomeClass.class) annotation argument
    • versus TestNG / TestNG versus JUnit summary
    • reference link / TestNG versus JUnit summary

K

  • kata exercise / The kata exercise
  • keep it simple, stupid (KISS)
    • about / Unit testing with TDD, Development practices
    • reference link / Keep it simple, stupid

L

  • legacy code
    • about / Legacy code
    • example / Legacy code example
    • recognition / Other ways to recognize legacy code
    • lack of dependency injection / A lack of dependency injection
  • legacy code algorithm, applying
    • about / Applying the legacy code algorithm
    • end-to-end test cases, writing / Writing end-to-end test cases
    • test cases, automating / Automating the test cases
    • BookRepository dependency, injecting / Injecting the BookRepository dependency
  • legacy code change algorithm / The legacy code change algorithm
  • legacy code change algorithm, applying
    • about / Applying the legacy code change algorithm
    • change points, identifying / Identifying change points
    • test points, finding / Finding test points
    • dependencies, breaking / Breaking dependencies
    • tests, writing / Writing tests
  • legacy kata
    • about / Legacy kata
    • Alexandria project / Legacy kata

M

  • markdown
    • reference link / Documentation for coders
  • Mars Rover / Remote-controlled ship requirements
  • method reference / map
  • Minimum Viable Product (MVP) / Narrative
  • mocking
    • about / Mocking, Mocking
    • features / Why mocks?
    • terminology / Terminology
    • mock objects / Mock objects
  • Mockito
    • about / Mockito

N

  • null class / Optional – dealing with uncertainty

O

  • Occam's razor principle
    • reference link / Occam's razor
  • Optional class
    • about / Example of Optional
    • example / Example of Optional
  • orientation, remote-controlled ship development
    • position, keeping in memory / Specification – keeping position and direction in memory
    • direction, keeping in memory / Specification – keeping position and direction in memory
    • implementation / Implementation
    • refactoring / Refactoring
  • override call / Breaking dependencies

P

  • partial mocking / Specification – adding items to the Mongo collection
  • players, Tic-Tac-Toe game
    • player X / Test – X plays first, Implementation
    • player O / Test – O plays right after X, Implementation
    • X's turn, checking / Test – X plays right after O
  • police syndrome / Better tests
  • Postman
    • reference link / Writing end-to-end test cases
  • preliminary investigation
    • about / Preliminary investigation
    • candidates, finding for refactoring / How to find candidates for refactoring
    • new feature, introduction / Introducing the new feature
  • primitive obsession
    • reference link / Removing the primitive obsession with status as int
  • Principles of OOD
    • reference link / SOLID principles
  • product owner (PO) / Adding a new feature

Q

  • quality assurance (QA)
    • versus quality checking / The difference between quality checking and quality assurance
    • about / The difference between quality checking and quality assurance, Exploring the codebase

R

  • red-green-refactor process
    • about / Red-Green-Refactor, The Red-Green-Refactor process, TDD in a nutshell
    • test, writing / Writing a test
    • tests, executing / Running all the tests and confirming that the last one is failing, Running all the tests
    • last test failure, confirming / Running all the tests and confirming that the last one is failing
    • implementation code, writing / Writing the implementation code
    • refactoring / Refactoring
    • repeating / Repeating
  • remote-controlled ship
    • requirements / Remote-controlled ship requirements
  • remote-controlled ship development
    • about / Developing the remote-controlled ship
    • project setup / Project setup
    • helper classes / Helper classes
    • starting point / Requirement – starting point and orientation
    • orientation / Requirement – starting point and orientation
    • forward moves / Requirement – forward and backward moves, Specification – moving forward
    • backward moves / Requirement – forward and backward moves, Specification – moving backward
    • ship, rotating / Requirement – rotating the ship
    • commands / Requirement – commands
    • spheric maps, representing / Requirement – representing spheric maps
    • obstacles detection / Requirement – detecting obstacles
  • REpresentational State Transfer (REST) / Release procedure
  • requisites, Reverse Polish Notation (RPN)
    • invalid input, handling / Requirement – handling invalid input
    • single operations / Requirement – single operations
    • complex operations / Requirement – complex operations
  • RestAssured
    • reference link / Automating the test cases
  • Reverse Polish Notation (RPN)
    • about / Functions revisited, Kata – Reverse Polish Notation
    • requisites / Requirements
  • Rule feature, JUnit
    • reference link / Requirement 1 – placing pieces

S

  • Selenide
    • reference link / Selenium and Selenide
  • Selenium
    • reference link / Selenium and Selenide
  • ship rotation, remote-controlled ship development
    • specification, for turning left / Specification – turning left
    • specification, for turning right / Specification – turning right
  • software design / Why should we care about design?
  • software design principles
    • about / Design principles
    • You Ain't Gonna Need It principle / You Ain't Gonna Need It
    • Don't Repeat Yourself (DRY) principle / Don't Repeat Yourself
    • keep it simple, stupid (KISS) / Keep it simple, stupid
    • Occam's razor principle / Occam's razor
  • SOLID principles
    • single responsibility principle / SOLID principles
    • open-closed principle / SOLID principles
    • liskov substitution principle / SOLID principles
    • interface segregation principle / SOLID principles
    • dependency inversion principle / SOLID principles
  • specifications, Behavior-Driven Development (BDD)
    • documentation / Documentation
    • documentation, for coders / Documentation for coders
    • documentation, for non-coders / Documentation for non-coders
  • spheric maps, remote-controlled ship development
    • planet information / Specification – planet information, Implementation, Refactoring
    • map boundaries / Specification – dealing with map boundaries
  • Spring Boot
    • reference link / A Feature Toggle example
  • store moves, Tic-Tac-Toe v2
    • DB name, specifying / Specification – DB name, Implementation
    • name, defining for Mongo collection / Specification – a name for the Mongo collection, Refactoring
    • items, adding to Mongo collection / Specification – adding items to the Mongo collection
    • operation feedback, adding / Specification – adding operation feedback, Refactoring
    • error handling / Specification – error handling, Specification – error handling
    • clear state between games / Specification – clear state between games
    • drop operation feedback / Specification – drop operation feedback
  • store turn, Tic-Tac-Toe v2
    • collection, creating / Specification – creating new collection, Specification refactoring
    • current move, storing / Specification – storing current move
    • error handling / Specification – error handling
    • alternate players / Specification – alternate players, Exercises
  • strategy pattern
    • reference link / Identifying change points
  • Streams
    • about / Streams
    • reference link / Streams
    • filter function / filter
    • map function / map
    • flatMap function / flatMap
    • reduce function / reduce

T

  • TDD best practices
    • about / Best practices
    • naming conventions / Naming conventions
    • processes / Processes
    • development practices / Development practices
    • tools / Tools
  • test-driven development (TDD)
    • about / Why TDD?, Understanding TDD, TDD in a nutshell
    • red-green-refactor process / Red-Green-Refactor
    • features / Speed is the key
    • testable code design / It's not about testing
  • test-first implementation, of Connect 4 game
    • about / The TDD or test-first implementation
    • Hamcrest, using / Hamcrest
    • board / Requirement 1 – the game's board
    • discs / Requirement 2 – introducing discs
    • player shifts / Requirement 3 – player shifts
    • output / Requirement 4 – the game's output
    • win condition, scenarios / Requirement 5 – win condition (I), Requirement 7 – win condition (III), Requirement 8 – win condition (IV)
  • test-last implementation, of Connect 4 game
    • about / Test-last implementation of Connect 4
    • board / Requirement 1 – the game's board
    • discs / Requirement 2 – introducing discs
    • player shifts / Requirement 3 – player shifts
    • output / Requirement 4 – the game's output
    • win condition, scenarios / Requirement 5 – win conditions (I), Requirement 7 – win condition (III), Requirement 8 – win condition (IV)
  • test double / Mocking, Why not use unit tests exclusively?
  • testing
    • about / Testing
    • black-box testing / Black-box testing
    • white-box testing / White-box testing
    • quality checking, versus quality assurance / The difference between quality checking and quality assurance
  • TestNG
    • about / TestNG
    • @Test annotation / The @Test annotation
    • @BeforeSuite annotation / The @BeforeSuite, @BeforeTest, @BeforeGroups, @AfterGroups, @AfterTest, and @AfterSuite annotations
    • @BeforeTest annotation / The @BeforeSuite, @BeforeTest, @BeforeGroups, @AfterGroups, @AfterTest, and @AfterSuite annotations
    • @BeforeGroups annotation / The @BeforeSuite, @BeforeTest, @BeforeGroups, @AfterGroups, @AfterTest, and @AfterSuite annotations
    • @AfterGroups annotation / The @BeforeSuite, @BeforeTest, @BeforeGroups, @AfterGroups, @AfterTest, and @AfterSuite annotations
    • @AfterTest annotation / The @BeforeSuite, @BeforeTest, @BeforeGroups, @AfterGroups, @AfterTest, and @AfterSuite annotations
    • @AfterSuite annotation / The @BeforeSuite, @BeforeTest, @BeforeGroups, @AfterGroups, @AfterTest, and @AfterSuite annotations
    • @AfterClass annotation / The @BeforeClass and @AfterClass annotations
    • @Test(enable = false) annotation argument / The @Test(enable = false) annotation argument
    • versus JUnit / TestNG versus JUnit summary
    • reference link / TestNG versus JUnit summary
  • tests
    • separation, in Gradle / Tests separation
  • thimblerig service / Case study – Awesome Gambling Corp
  • Thymeleaf
    • reference link / A Feature Toggle example
  • Tic-Tac-Toe game
    • requirements / Tic-Tac-Toe game requirements
    • reference link / Tic-Tac-Toe game requirements
    • developing / Developing Tic-Tac-Toe
    • pieces, placing / Requirement 1 – placing pieces
    • board boundaries / Test – board boundaries I, Implementation, Test – board boundaries II, Implementation
    • pieces, placing on unoccupied spaces / Test – occupied spot, Implementation, Refactoring
    • two players support, adding / Requirement 2 – adding two-player support
    • win conditions, adding / Requirement 3 – adding winning conditions, Test – by default there's no winner, Implementation, Test – winning condition I, Implementation, Refactoring, Test – winning condition II, Implementation, Test – winning condition III, Implementation, Test – winning condition IV, Implementation, Refactoring
    • tie condition / Requirement 4 – tie conditions, Test – handling a tie situation, Implementation, Refactoring
    • code coverage / Code coverage
    • integration test / The integration test
  • Tic-Tac-Toe v2 game
    • requirements / Tic-Tac-Toe v2 requirements
  • Tic-Tac-Toe v2 game, developing
    • about / Developing Tic-Tac-Toe v2
    • moves, storing / Requirement 1 – store moves
    • turn, storing / Requirement 2 – store every turn
  • Togglz
    • reference link / Feature Toggles
  • Travis
    • reference link / Processes

U

  • unit testing
    • about / Unit testing
    • need for / Why unit testing?
    • limitations / Why not use unit tests exclusively?
    • with test-driven development (TDD) / Unit testing with TDD
  • user acceptance tests / Test-last implementation of Connect 4

W

  • white-box testing
    • about / White-box testing
    • advantages / White-box testing
    • disadvantages / White-box testing

Y

  • YAML Ain't Markup Language (YAML) / A Feature Toggle example
  • You Ain't Gonna Need It principle
    • reference link / You Ain't Gonna Need It