How can we test an actor with out going http request

182 views
Skip to first unread message

Avi

unread,
May 24, 2015, 4:52:10 AM5/24/15
to akka...@googlegroups.com
Hi ,
I have an actor that is sending outbound http request.
I would like top  test that actor ,is there an option to mock that call ? what is the correct way for doing that ? 

Best
Avi

Giovanni Alberto Caporaletti

unread,
May 25, 2015, 3:53:17 AM5/25/15
to akka...@googlegroups.com
Try to separate your login between internal business actors that you can unit test and a gateway sub-system to connect to external systems. 
The gateway would translate all the connection logic from the external domain to your internal domain, including errors and other possible states.  You could need to handle state and/or any kind of retry logic/failure compensation etc.

The external system (http) would appear as a message-driven actor to your bounded context, speaking your domain language, thus being unit testable.
The gateway subsystem will be independently integration-tested. You could have a mock http server in your integration tests to verify the external calls and the translation.

Cheers
G

Konrad Malawski

unread,
May 31, 2015, 6:00:54 PM5/31/15
to Akka User List
I agree with Giovanni's hints here - your "domain actors" should talk using your "domain language" (the messages).
Of course you can always just create an HttpRequest message (see http model in the docs) if you want to send that,
however I'd recommend to test the http things more for the routing and checking if the proper domain message is sent 
as reaction to such http request (check out our routing testkit: https://github.com/akka/akka/blob/release-2.3-dev/akka-http-testkit/src/test/scala/akka/http/scaladsl/testkit/ScalatestRouteTestSpec.scala ), and then test domain things with normal domain messages.

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,
Konrad 'ktoso' Malawski

Avi Levi

unread,
May 31, 2015, 8:15:39 PM5/31/15
to akka...@googlegroups.com
Thank you very much Giovanni .
I will try to follow that 
Best
Avi

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/-rsD467ADC4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.

Avi Levi

unread,
May 31, 2015, 8:53:05 PM5/31/15
to akka...@googlegroups.com
Thank you Konrad .
I am not sure I got you. 
IIUC you suggest to create an "external" service actor that will accept messages and rout them to the proper external API and will send the response back . if that is correct then again how can I test it ? the testkit allow me to test incoming requests to my API .
I might didn't explain myself correctly or I am missing something .
my actor needs to call some external api . something like this :
class MyActor extends Actor {
def receive = {
case GetCities(countryCode:String) => //do http request from external system to get the cities 
 }
}  




You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/-rsD467ADC4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.

Roland Kuhn

unread,
Jun 2, 2015, 7:47:40 AM6/2/15
to akka-user
Hi Avi,

in order to switch out the actual HTTP part you’ll have to configure your Actor with this part, e.g. by providing a function “HttpRequest => Future[HttpResponse]” or a “Flow[HttpRequest, HttpResponse, _]” or similar. Then in your production code you plug in the HTTP client pool and in your tests you can simply stub it out by a function that provides canned answers.

Regards,

Roland


Dr. Roland Kuhn
Akka Tech Lead
Typesafe – Reactive apps on the JVM.
twitter: @rolandkuhn


Reply all
Reply to author
Forward
0 new messages