Richard Searle

home

Unit testing Akka future based choreography

17 Jan 2012

The previous post described a choreography implementation using Futures.

All technologies need to consider testability, which is thus the topic of this post.

The first step is a completely self contained unit test mechanism. This allows the testing of the business logic embodied in the service, without the complexity of the concurrent runtime environment.

A specs2 test for the Balance service might be

"balance" in {
    Balance(Acct("alpha")).get must beEqualTo(Bal(124.5F))
  }

Note the get which blocks until the future returns its result.

The Balance service requires a suitable mock implementation of Lookup[Acct, Bal].

implicit val balLook: Lookup[Acct, Bal] = Service(Map(Acct("alpha") -> Bal124.5F))


private abstract class MapService[K, V](map: Map[K, V]) {
    self: Function1[K, Future[V]] =>
    def apply(a: K) = Future(map(a))
}

private case class Service[K, V](values: Map[K, V]) extends MapService(values) with Lookup[K, V]

The implicits eliminate the need to explicitly reference the balLook, much like Spring auto wiring by type.