Book Image

Scala Test-Driven Development

By : Gaurav Sood
Book Image

Scala Test-Driven Development

By: Gaurav Sood

Overview of this book

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.
Table of Contents (16 chapters)
Scala Test-Driven Development
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface

Eventually


The Eventually trait provides an eventually method, which can repeatedly execute a function passed by name. This is done until the passed function succeeds or the timeout, which was configured, has been exceeded.

The function, which was passed by name, is considered to "succeed" when it returns a result. If the function throws an exception, then eventually will "fail" and would result in the failure of the test.

Let's look at a few examples of eventually. Let's see a successful example first:

val alphabets = 'a' to 'z'
val iterator = alphabets.iterator
eventually { iterator.next should be ('p') }

The default timeout for eventually is 150 milliseconds. This means that if we use the following code, it would result in TestFailedDueToTimeoutException:

val alphabets = 'a' to 'z'
val iterator = alphabets.iterator
eventually { Thread.sleep(50); iterator.next should be ('p') }

How to configure eventually

The configuration of the eventually method is quite flexible. There...