Scala Test-Driven Development

Book description

Build robust Scala applications by implementing the fundamentals of test-driven development in your workflow

About This Book

  • Get a deep understanding of various testing concepts such as test-driven development (TDD) and BDD
  • Efficient usage of the built-in Scala features such as ScalaTest, specs2, and Scala check
  • Change your approach towards problem solving by thinking about the boundaries of the problem and its definition rather than focusing on the solution

Who This Book Is For

This book is for Scala developers who are looking to write better quality and easily maintainable code. No previous knowledge of TDD/BDD is required.

What You Will Learn

  • Understand the basics of TDD and its significance
  • Refactoring tests to build APIs in order to increase test coverage
  • How to leverage the inbuilt Scala testing modules like ScalaTest, specs2 and Scala Check
  • Writing test fixtures and apply the concepts of BDD
  • How to divide tests to run at different points in continuous delivery cycle
  • Benefits of refactoring and how it affects the final quality of code produced
  • Understanding of SBT based build environment and how to use it to run tests
  • The fundamentals of mocking and stubbing in Scala and how to use it efficiently

In Detail

Test-driven development (TDD) produces high-quality applications in less time than is possible with traditional methods. Due to the systematic nature of TDD, the application is tested in individual units as well as cumulatively, right from the design stage, to ensure optimum performance and reduced debugging costs.

This step-by-step guide shows you how to use the principles of TDD and built-in Scala testing modules to write clean and fully tested Scala code and give your workflow the change it needs to let you create better applications than ever before.

After an introduction to TDD, you will learn the basics of ScalaTest, one of the most flexible and most popular testing tools around for Scala, by building your first fully test-driven application. Building on from that you will learn about the ScalaTest API and how to refactor code to produce high-quality applications.

We'll teach you the concepts of BDD (Behavior-driven development) and you'll see how to add functional tests to the existing suite of tests. You'll be introduced to the concepts of Mocks and Stubs and will learn to increase test coverage using properties.

With a concluding chapter on miscellaneous tools, this book will enable you to write better quality code that is easily maintainable and watch your apps change for the better.

Style and approach

This step-by-step guide explains the significance of TDD in Scala through various practical examples. You will learn to write a complete test-driven application throughout the course of the book.

Table of contents

  1. Scala Test-Driven Development
    1. Scala Test-Driven Development
    2. Credits
    3. About the Author
    4. Acknowledgments
    5. About the Reviewer
    6. www.PacktPub.com
      1. Why subscribe?
    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. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Hello, TDD!
      1. What is TDD?
      2. Why TDD?
      3. Changing our approach to problem solving
        1. Iteratively writing failing tests
          1. Baby steps
      4. Brief introduction to Scala and SBT
        1. What is Scala?
        2. Why Scala?
        3. Scala Build Tool
        4. Why SBT?
        5. Resources for learning Scala
        6. Resources for SBT
        7. Setting up a build environment
        8. Steps for downloading and installing Scala
        9. Steps for downloading and installing SBT
        10. Creating a project directory structure
        11. Build definition files
      5. Hello World!
        1. Creating a directory structure
        2. Creating a build definition
        3. Test first!
      6. Summary
    9. 2. First Test-Driven Application
      1. Testing frameworks
      2. ScalaTest
        1. A quick tutorial
          1. Adding ScalaTest to the project
          2. Choose your testing style
            1. FunSuite
            2. FlatSpec
            3. FunSpec
            4. WordSpec
            5. FreeSpec
            6. Spec
            7. PropSpec
            8. FeatureSpec
        2. Resources for ScalaTest
      3. Problem statements
      4. IDE
      5. Project structure
      6. Write a failing test – RED
      7. Writing application code to fix the test – GREEN
      8. More tests – REPEAT
      9. Intercepting exceptions
      10. Summary
    10. 3. Clean Code Using ScalaTest
      1. Assertions
        1. Deliberately failing tests
        2. Assumptions
        3. Canceling tests
        4. Failure messages and clues
      2. Matchers
        1. Matchers for equality
        2. Matchers for instance and identity checks of objects
        3. Matchers for size and length
        4. Matching strings
        5. Matching greater and less than
        6. Matching Boolean properties
        7. Matching number within ranges
        8. Matching emptiness
        9. Writing your own BeMatchers
      3. Some more Matchers
        1. Matchers for containers
        2. Combining Matchers with logical expressions
        3. Matching options
        4. Matching properties
        5. Checking that a snippet of code does not compile
      4. Base test classes
      5. Test fixtures
        1. Calling get-fixture methods
        2. Instantiating fixture-context objects
        3. Overriding withFixture(NoArgTest)
        4. Calling loan-fixture methods
        5. Overriding withFixture(OneArgTest)
        6. Mixing in BeforeAndAfter
        7. Composing fixtures by stacking traits
      6. Problem statement
        1. Feature – decimal to hexadecimal conversion
          1. BinaryToDecimalSpec.scala
          2. HexadecimalToDecimalSpec.scala
          3. DecimalHexadecimalSpec.scala
          4. BeanSpec.scala
          5. package.scala.packt
          6. BaseConversion.scala
      7. Summary
    11. 4. Refactor Mercilessly
      1. Clean code
      2. Red-Green-Refactor
      3. Code smell
        1. Expendable
        2. Couplers
        3. Modification thwarters
        4. Bloaters
        5. Object-oriented abusers
        6. Obsolete libraries
      4. To refactor or not to refactor
        1. Doing it thrice (rule of three)
        2. Adding new feature
        3. Bug fixing
        4. Code reviews
      5. Refactoring techniques
        1. Composing methods
        2. Moving features between objects
        3. Organizing data
      6. Summary
    12. 5. Another Level of Testing
      1. Integration testing
        1. Functional testing
        2. Acceptance testing
        3. Need for user acceptance testing
      2. Behavior-driven development
        1. Introduction
        2. Three amigos
        3. Bird's-eye view of BDD
        4. Gherkin
        5. Executable specification
        6. Don't repeat yourself
      3. Talk is cheap
        1. Adding Cucumber dependency
        2. Directory structure
        3. The feature file
        4. Running the feature
        5. Step definition
      4. Summary
    13. 6. Mock Objects and Stubs
      1. History
      2. Coupling
      3. Stubs
      4. Mock objects
        1. Expectations
        2. Verifications
      5. Fakes
      6. Spy
      7. Mocking frameworks
        1. JMock
          1. Advantages of JMock
        2. EasyMock
      8. Mockito
      9. ScalaMock
        1. Advantages of ScalaMock
        2. mockFunction
        3. Proxy mocks
        4. Generated mocks
        5. Let's dig a little deeper
          1. Specifying expectations
      10. Summary
    14. 7. Property-Based Testing
      1. Introduction to property-based testing
        1. Table-driven properties
      2. Generator-driven properties
      3. ScalaCheck
        1. Generators
          1. Generating case classes
          2. Conditional Generators
          3. Generating containers
          4. Arbitrary Generator
          5. Generation statistics
        2. Executing property checks
        3. Our own Generator-driven property checks
      4. Summary
    15. 8. Scala TDD with Specs2
      1. Introduction to Specs2
      2. Differences between Specs2 and ScalaTest
      3. Setting up Specs2
      4. Styles
        1. Unit specifications
      5. Acceptance specification
      6. Matchers
        1. Simple Matchers
        2. Matchers for strings
        3. Matchers for relational operators
        4. Matchers for floating point
        5. Matchers for references
        6. Matchers for Option/Either
        7. Matchers for the try monad
        8. Matching exception
        9. Iterable Matchers
        10. Matchers for sequences and traversables
        11. Matchers for maps
        12. Matchers for XML
        13. Matchers for files
        14. Matchers for partial functions
        15. Other Matchers
      7. Specs2 data tables
      8. Running Specs2 tests
      9. Summary
    16. 9. Miscellaneous and Emerging Trends in Scala TDD
      1. Scala Futures and Promises
        1. ExecutionContext
        2. Futures
      2. The Inside trait
      3. The OptionValue trait
      4. The EitherValue trait
      5. Eventually
        1. How to configure eventually
        2. Simple backoff algorithm
        3. Integration patience
      6. Consumer-Driven Contracts
        1. How services interface with each other
          1. The gigantic way
          2. Microservices
        2. Using CDC to integrate microservices
          1. Traditional way
          2. Using CDC
        3. Benefits of CDC
      7. Summary

Product information

  • Title: Scala Test-Driven Development
  • Author(s): Gaurav Sood
  • Release date: October 2016
  • Publisher(s): Packt Publishing
  • ISBN: 9781786464675