Richard Searle's Blog

Thoughts about software

Unit testing Akka future based choreography

Posted by eggsearle on January 17, 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: