Hi. Specs2 is one of the most popular Scala libraries for testing, but definitely not a simple one. That’s why I decided to create a blog post with a few tricks how to make working with Specs2 much easier. The examples are 100% artificial, but their purpose is only to demonstrate capabilities of Specs2.

Basic example

Ok, so let’s start with a basic example

Pass data into the tests

We can do this in (at least) two ways. Just pass the data as regular variables

or pass them as a scope

The first option is very straightforward and works well for small tests, but is impractical for bigger ones or when you want to (for some reason) share the values.

Implement custom Setup and Teardown

Now let’s say that we need to perform some important operations before and after every single test case. How can we achieve that? It’s simple. We can use Contexts again.

First we need to create an helper for local setup and Teardown. We can do it like this

now we can do

Implement custom Setup and Teardown for all test cases

The example above works good but only for single test cases, now lets write something for the whole test suite.

Random thoughts

  1. Watch out for static configuration and methods, they might cause tests to break from time to time.
  2. When using Specs2 try not to mix sub-packages. Some mixins from org.specs2.mutable might not work those from org.specs2.specification and so one. Even worse they might just fail silently.
  3. As you probably noticed already Contexts can be classes, traits and objects.

Do you like this post? Want to stay updated? Follow us on Twitter or subscribe to our Feed.

Akka Streams is an exciting new technology from Typesafe that is an implementation of the Reactive Streams specification. To this point two other implementations have been declared (from the Reactor team and Netflix’s RxJava team) but only the Typesafe’s implementation is mature enough to do some experimenting with it. This blog covers the 0.3 version of Akka Streams. You can find the API here

RabbitMQ is a messaging broker implementing AMQP 0-9-1 protocol. It’s known for its reliability, speed and simplicity in everyday use.

These two technologies seem like a perfect fit, so in this post I’m going to explore some basic integration possibilities and example usage. Read more