1
votes

I have an application utilizing Spray, Akka, and Scala. The current unit testing is done via Scala Test. The application uses Spray routing to determine and parse some rudimentary data on web requests then passes it to an actor to do the required actions. For Spray we use a custom initialization class that inherits from spray.servlet.Initializer which configures and starts each of the actors. Part of those actions are to call out to some 7 or 8 other web services. Each has an actor to handle the communications with their respective services. So, we do a bunch of logic in the main actor which delegates the communication to other actors and at the end it processes all of the returned data in addition to it's own work along the way.

I would like to test the system as a whole using Scala Test and Akka Testkit using Testkit to substitute the communication actors to return suitable test data.

The question is two part.

  • What is the better approach to testing? I could use Scala Testkit to make requests through the Spray routing service via Spray Testkit. The alternative is, since the main actor takes the routing service results via a case class, is to just to directly pass the message to that actor skipping the routing service. Both have their merits. I however do find the documentation scant on Spray testkit. How would one sub the actors via Akka Testkit when there is initialization logic for those actors in spray.servlet.Initializer?

  • The second is how does one set up a more complex actor system via Akka Testkit. The documentation mentions this is possible but is far from expressive on exactly how one would do that. I have the routing service which is an actor that talks to another actor that is the bulk of business logic but then talks to several other actors. Are these communication actors considered "Child" actors in reference to the Akka Testkit documentation? Is there a project that demonstrates best practices in testing a rich Akka actor system as a whole?

My instincts in this case are to have a Spray Testkit based set of tests to test our routing system. Then, have a set of tests that send our data case class to the master actor with mocked comm actors behind it and verify we get the correct response back from the master actor.

1

1 Answers

1
votes

I usually create tests for each layer of my application. Also, i mock the other layer when i'm testing the current layer. If i'm testing business i would mock the DAOs, if i'm testing spray routes i mock the business object(that is used by my spray routes). I always try to start creating tests before the main program when i'm working with Actors and Spray, it helps in how should be my application architecture. Many times i need to refactor my class to use dependency injection or do not set the val in the current class/trait, so i can mock the vals.