So, You want to write better unit and integration test cases then You have got read this blog. Alright, Let’s see what it takes to test your code in Lagom. In this blog I will be taking up different scenarios and try to explain How to write test cases for that particular scenario.

Testing in Lagom

Testing in Lagom is not a rocket science. However, It is a little different the way we test our code in Scala and Java. We will be using Junit framework(recommended by Lagom) and along with this we will be using lagom’s default testkit dependency. In order to test a service in Lagom we will basically be starting up a fake server which will make calls to our services and produce the result so that we could assert it against the expected result. Also, with fake server you have a flexibility to use embedded cassandra and cluster. You can set embedded cassandra to true whenever you need it, similarly clustering can also be set to true

For example your code needs cassandra the you will say withCassandra(true), bydefault it is false. We will see this in detail when we will cover all the scenarios which require cassandra to be true.

Alright, So Let’s get started

Basic Junit Test

Writing a basic Junit test-case is very easy.

Prerequisites

Add Junit maven dependency

junit

junit

4.11

test

@Test

publicvoidtestSum() {

inta = 1+ 1;

assertEquals(2, a);

}

2. Lagom Service Unit Test

Writing a Lagom Service Unit Test-case is very ease, All you need to do is to run a test-server in @BeforeClass and then tear down that server in @AfterClass

Prerequisites

1.Add maven dependency for writing lagom service testcases in POM.XML

Maven Dependency

<dependency>

<groupId>com.lightbend.lagom</groupId>

<artifactId>lagom-javadsl-testkit_2.11</artifactId>

<version>${lagom.version}</version>

<scope>test</scope>

<exclusions>

<exclusion>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

</exclusion>

</exclusions>

</dependency>

Sample Test-Case

Lagom Service Unit Test

publicclassWelcomeServiceTest {

privatestaticServiceTest.TestServer server;

@BeforeClass

publicstaticvoidsetUp() {

server = startServer(defaultSetup().withCassandra(false));

}

@AfterClass

publicstaticvoidtearDown() {

if(server != null) {

server.stop();

server = null;

}

}

@Test

publicvoidshouldWelcome() throwsException {

WelcomeService welcomeService = server.client(WelcomeService.class);

String actualResult = welcomeService.welcome().invoke().

toCompletableFuture().get(20, SECONDS);

String expectedResult = "Welcome to MyService!";

assertEquals(expectedResult, actualResult);

}

}

3. Lagom Stream Test-Case

Stream Test-Case

publicclassEchoServiceTest {

@Test

publicvoidshouldEchoStream() throwsException {

withServer(defaultSetup().withCassandra(true), server -> {

EchoService service = server.client(EchoService.class);

// Use a source that never terminates (concat Source.maybe) so we

// don't close the upstream, which would close the downstream

Source<String, NotUsed> input =

Source.from(Arrays.asList("msg1", "msg2", "msg3"))

.concat(Source.maybe());

Source<String, NotUsed> output = service.echo().invoke(input)

.toCompletableFuture().get(20, SECONDS);

Probe probe = output.runWith(TestSink.probe(server.system()),

server.materializer());

probe.request(10);

probe.expectNext("msg1");

probe.expectNext("msg2");

probe.expectNext("msg3");

probe.cancel();

});

}

}

4. Service Stub Test

In this test case, We will be writing a test-case wherein we will be stubbing a service into another service for writing a unit test. Prerequisites 1. In order to to write the unit test for the above scenario, We will stub another service in our service Sample Test File

completedFuture("Welcome service is being consumed inside Hello service!");

}

}

}

11. Consuming External Service (Integration Test)

In this scenario, We will be writing integration test for the service which is consuming external web service. In this case we will be making direct calls to the external service via Lagom Micro-Service. While the external web service is up and running so that direct calls can be made to check the functionality end to end.