This book started off as a magazine article for a popular conference, No Fluff, Just Stuff. The article became a presentation, then the presentation became a book. It became evident early on that Scala had something good going on when it came to testingnot only with its variety of quality open source software, but also with automated test generation.
This book revolves around music, albums, artists, and bands. It makes the topics less dry, even though testing is wonderfully exciting, and it includes music from different generations. So anyone alive today will likely encounter a band or an artist that they will like. Music is universal, and relatable to most people. Using music in techical books as examples is not new: two of my favorite OReilly titles, Hibernate: A Developers Notebook and Learning the bash Shell, 3rd Edition used music in some of the examples, and I loved the idea so much I use it in constantly in teaching, in speaking, and of course in writing.
Much of the production code is simplesome might say pedestrian. The intent of the book is not to impress with overly fanciful or verbose production code, but to focus on testing code. As for the testing code, I also try to keep that simple, but I always provide some extra explanation if the code becomes unfamiliar or esoteric.
Audience
This book assumes some Scala knowledge, but recognizes that readers might not know all the nooks and crannies of the language. Therefore, all that is required is basic familiarity. And some Ruby and Python programmers may wander over to learn something different. For those groups, perhaps a quick introduction to Scala is in order. This may be fairly simple for Ruby and Python developers. I believe they are more apt to understand Scala concepts than Java programmers, since many of Scalas language constructs have been used in Ruby and Python for years.
If the reader still does not feel that comfortable with Scala, either visit the Scala website for tutorials, read Dean Wampler and Alex Paynes book, Programming Scala (OReilly), peruse the Daily Scala blog or attend some great conferences, many hosted by OReilly, that cover Scala.
Another learning opportunity is learning Scala through Scala Koans. Koans are small, Zen-like interactive lessons, meant to foster learning without overwhelming detail. Each lesson is short and comes with its own bite-sized epiphany. New koans are added all the time, and is a fantastic way to learn the language. The koans by yourself which is the lonely way to go, or at a local conference where it is interactive and conducive to more questions and answers.
Organization of This Book
, Setup This chapter is about setting up a sample project to be used in the book. , Structure and Configuration of Simple Build Tool (SBT) This chapter consists of a slightly deeper introduction to Simple Build Tool, an open source, Scala based tool and competitor to ant
, maven
, and gradle
. This chapter covers basic commands, using interactive mode, packaging, and using SBTs history. , ScalaTest This chapter shows how to use ScalaTest both on the command line and with SBT. The chapter covers how to use the different specifications, how to tag tests, how to use MustMatchers
and ShouldMatchers
domain-specific languages (DSLs), and how to incorporate some of the popular Java-based frameworks, like JUnit and TestNG. This chapters also covers strategies for creating test fixtures with ScalaTest. , Specs2 Specs2 is an alternative testing framework that covers its two styles of specifications, unit and acceptance. This chapter delves into its own matcher DSLs, how to use data tables, and how to tag tests. The chapter also covers its own strategies for creating test fixtures with Specs2. , Mocking This chapter covers mocking, the art of substituting large subsystems with objects rehearsed to perform your will to make Scala unit tests isolated. This chapter covers the Java mocking frameworks EasyMock and Mockito, and how they interact with Scala. This chapter will also cover how to use ScalaTests sugar to incorporate EasyMock with ScalaTest, and how to use Specs2 sugars with Mockito. Finally, this chapter covers a home-grown mocking framework called ScalaMock, which supports mocking for some of the toughest constructs to mocklike functions, companion and singleton objects, and final classes and methods. , ScalaCheck This chapter covers an amazing tool that generates fake data for tests and does so thoroughly by creating a set of fake data for tests. This chapter covers how to manipulate ScalaCheck to give you the test data needed for effective unit testing. Finally, the chapter wraps up by showing some ScalaCheck sugars that are available in ScalaTest and Specs2.
About the Book
This book enhances the Scala language with standard test-driven development practices, highlighting the best testing tools today. This book will cover both the ScalaTest and the Specs2 testing frameworks, which help you create quick and easy tests. Testing is also often the most overlooked aspect of introductory programming language books. This book is dedicated to mending that gap.
We will run all these tests using Simple Build Tool (SBT). SBT is similar to some earlier build tools and competitors: Maven, Gradle, and Buildr. What makes SBT highly attractive is its ease of use and the small size of the build file. Type a few lines of code for your build file and youre off and running on your project. We will also cover SBTs wonderful triggered execution feature, which complements test-driven development by building and testing code whenever a file is saved.
ScalaTest and Specs2 are two of the most dominant testing frameworks for Scala around today. Each framework has a different intent and goal, but they share the same ideal of making testing concise, and they both leverage the Scala programming language to make testing easy and fun. Testing frameworks are nothing new, of course, and have been used with other programming languages for years. Those familiar with other programming languages and their testing tools will find some similarities with Scalas current testing tools. ScalaTest and Specs2 borrowed ideas from Cucumber. But upon these shoulders of giants, Scala testing systems have also stepped out on their own and created some of the most mind-blowing testing tools found in any language.
Testing in Scala will also illustrate mocking code, so as to keep our tests isolated from large subsystems and networks. Mocking is, in essence, creating a substitute for various objects to isolate tests from volatile elements of their environment (such as the contents of databases) and to help unit tests run fast. This book shows how you can use Scala with Java-based mocking frameworks that have been used for years by Java programmers, EasyMock and Mockito. We will also introduce you to a new framework, ScalaMock. Formerly known as Borachio, ScalaMock was inspired by Javas EasyMock and Mockito but takes their work further, even offering support for mocking final classes and Scala objects.
Following mocking, we will also generate a massive battery of prefabricated test data using Scala Check, which is borrowed heavily from the Haskell programmed testing framework called QuickCheck. Scala Check has preconfigured formulas to generate strings, numbers, and other various objects automatically. Scala Check also offers formulas to generate your own custom test objects.
This book will be organized in a TDD fashion: test first, fail; test again, succeed maybe; test again, succeed, and so on.
Because Scala is a deep forest of coding possibilities, my intent is to start on familiar ground, with the imperative programming paradigm, and work our way to the Scala functional programming paradigm, discovering some things about functional programming along the way. I will describe some Scala calls that may be obscure, either to introduce you to some constructs that you may not be familiar with, or as a refresher for those that are familiar with Scala.