Test-Driven Java Development
eBook - ePub

Test-Driven Java Development

Viktor Farcic, Alex Garcia

Share book
  1. 284 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Test-Driven Java Development

Viktor Farcic, Alex Garcia

Book details
Book preview
Table of contents
Citations

About This Book

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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Test-Driven Java Development an online PDF/ePUB?
Yes, you can access Test-Driven Java Development by Viktor Farcic, Alex Garcia in PDF and/or ePUB format, as well as other popular books in Informatique & Programmation en Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2015
ISBN
9781783987429

Test-Driven Java Development


Table of Contents

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

Test-Driven Java Development

Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any...

Table of contents