Chapter 10. Building confidence with testing
This chapter covers
- Automated testing using ScalaCheck
- Using JUnit to test Scala code
- Writing better tests with dependency injection
- Behavior-driven development using Specs
- Testing actor-based systems
So far, I’ve been showing you code without writing tests—so why worry about that right now? The answer is, I wrote tests around the code but didn’t mention doing so because I wanted you to focus more on the Scala language. Now that’s going to change. My goal for this chapter is to make you comfortable writing automated tests in Scala so that you can build production-quality software.
The path to writing well-crafted code[1] is the path where you write tests for your code. The common perception about writing tests is that it’s hard, but this chapter will change that mindset. I’m going to show you how you can get started with practices like test-driven development and continuous integration for your Scala project. The idea of test-driven development (TDD) is to write the test before you write code. I know this seems backward, but I promise you that by the end of this chapter it will make sense. You’ll learn that writing tests is more like doing a design exercise than testing, and it makes sense to design your software. Your design tool will be code—more specifically, test code.
1 “Manifesto for Software Craftsmanship,” http://manifesto.softwarecraftsmanship.org.