Scala is a functional language, and many of your tests can stress functions without having the set-up complex state. ScalaCheck does just this.3 While ScalaCheck is not part of Specs, Specs wraps ScalaCheck. It s super-simple to use ScalaCheck inside Specs test. It generates random input based on rules and tests your method against the random input. For example, this will test 500 e-mail addresses against the pattern:

Using Barcode encoder for Font Control to generate, create Barcode image in Font applications.

www.OnBarcode.com

Specs test can be read by and often written by business users of your code. This is important because the more transparent the tests, the more likely your application will perform the way that business people expect it to, and when that happens, you get fewer tickets filed and more time to write new, cool code or drink a beer.

Using Barcode encoder for Android Control to generate, create ECC200 image in Android applications.

www.OnBarcode.com

Scala s software ecosystem is rich, and there s more than one excellent testing framework. ScalaTest4 is written and maintained by Artima5 founder and Programming in Scala co-author Bill Venners. Bill has taken a slightly more Java-flavored approach in ScalaTest but also offers the BDD goodness that Specs has. Bill says that his goal for ScalaTest is that It really tries to . . . integrate with the past while at the same time letting you adopt new styles that Scala enables better than Java did. People are comfortable doing things the way they have been doing them, and I want to make the transition as gentle as possible. For example, a simple ScalaTest test looks like:

This looks to me like a more pleasant version of the kind of tests that I d write in JUnit. In fact, ScalaTest has excellent integration with JUnit, so you can use JUnit as your test runner. ScalaTest has TestNG integration as well. ScalaTest wraps ScalaCheck just as Specs does, and you can write BDD-style tests using ScalaTest as well:

ScalaTest and Specs both offer integration with Ant and Maven, so you can integrate your tests into your existing Java build environment. With both tools, you can describe tests for your business logic in simple, readable, powerful ways and run those tests using your existing build and test infrastructure. In this way, Scala integrates very well into your organization.

Build Tools

Scala works with most popular Java-related build tools including Maven, Ant, and Buildr. This means that integrating Scala into an existing Java-oriented workflow is simple and painless. In this section, I m going to do a quick survey of the tools available for building, testing, and packaging Scala apps, and that includes apps that contain Scala, Java, JRuby, and so on. Basically, these tools convert source code to bytecode and stuff that bytecode into JAR and WAR files. The order I ve chosen is most verbose to least verbose.

Maven

My personal favorite build and dependency-management tool of all time is Maven (I duck as people throw things at me.6) Maven is far more than a build tool: it s a complete dependency-management system and will even ensure that you don t mix items in a project where there are license conflicts (e.g., GPL 2 and Apache 2.0). Maven arranges things as dependencies and goals. Maven will figure out what dependencies are necessary to resolve in order to achieve a goal. Maven may need to download a JAR file in order to resolve a dependency. Maven will go to different repositories, including well-known repositories of open source code, in order to download dependencies. Maven also determines what code to run in order to resolve a dependency in order to reach a goal. For example, if the goal is jetty:run (run the web application inside a Jetty container), Maven will download the JARs that your code depends on, compile your source code, package your code into a WAR directory layout, download the Jetty runtime, and then invoke Jetty. With Maven, you only worry about your own code and defining its dependencies, and Maven takes care of the rest.