Hi Tim,
You should be able to mock a service call by just implementing it. You could also use Mockito if you don't want to implement every method on it. But let's say you have this interface for ProductService, and this implementation for OrderService, and the following application cake:
trait ProductService {
def getProduct(id: String): ServiceCall[NotUsed, Product]
override def descriptor = ...
}
class OrderServiceImpl(productService: ProductService)(implicit ec: ExecutionContext) extends OrderService {
override def addProductToOrder(orderId: String, productId: String) = ServiceCall { _ =>
productService.getProduct(productId).map { product =>
...
}
}
}
class OrderApplication(ctx: LagomContext) extends LagomApplication(ctx)
with AhcWSComponents {
lazy val productService = serviceClient.implement[ProductService]
override lazy val lagomServer = LagomServer.forServices(bindService[OrderService].to(wire[OrderServiceImpl]))
}
Now, in your test you want to mock product service, so you simply override it in your cake:
class OrderServiceTest extends WorkSpec with Matchers with BeforeAndAfterAll {
lazy val server = ServiceTest.startServer(ServiceTest.defaultSetup) { ctx =>
new OrderApplication(ctx) with LocalServiceLocator {
// Here is your mocked product service. If you would rather use mockito,
// you could also at this point return a mockito implemented product
// service.
override lazy val productService = new ProductService {
override def getProduct(id: String) = ServiceCall { _ =>
id match {
case "123" => Future.successful(new Product(...))
case "456" => throw NotFound()
}
}
}
}
lazy val client = server.serviceClient.implement[OrderService]}
...
}
Regards,
James