A JUnit 5 Extension for testing Akka and Akka Streams
1 June 2018
Akka is an amazing toolkit which has an implementation of the actor model (akka-actors) as well as the reactive streams (akka-streams) and already comes with a handy test kit as well.
However for using the test kit you will still need to setup Akka components like ActorSystem
or ActorMaterializer
yourself.
In order to make this a bit more convenient using JUnit I decided to create a small extension takes care of the lifecycle of those two components during test.
JUnit 5 extensions allow you to setup your testing harness. It provides certain hooks which are called before, during and after the tests, your extension can use a context object in which it can store its state. An extension can resolve test method parameters effectively injecting the components it is managing into each test method.
The little AkkaJunitExtension
I have created can easily be used.
The following test is a short demonstration of that:
@ExtendWith(AkkaJunitExtension.class)
@AkkaJunitExtensionConfig(name = "test")
class SomeAkkaJunit5Test {
@Test
void system_is_present(ActorSystem system){
assertThat(system).isNotNull();
}
@Test
void system_has_name(ActorSystem system){
assertThat(system.name()).isEqualTo("test");
}
@Test
void actormaterializer_is_present(ActorMaterializer mat) {
assertThat(mat).isNotNull();
}
}