Skip to content

Unit Testing

Alvin Reyes edited this page Dec 27, 2016 · 3 revisions

FB-BotMill offers a powerful framework for testing your bot without even deploying it. In order to do that, you can use the FaceBotMockMediator class. Here's an example using jUnit:

@Test
public void test(){
FaceBotMockMediator mediator = new FaceBotMockMediator("12345678912345", MyBehavior.class);
mediator.sendTextMessage("Hello!");
    mediator.sendPayload("MyCustomPayload");
}

The FaceBotMockMediator constructor takes as arguments a String which represents your page-scoped Facebook ID and one or more classes (or actual instances) of your FaceBotDefinition.

Notice that the page-scoped Facebook ID is NOT the same as your Facebook ID. At the moment I'm writing this, the only method I know to get your ID is through FaceBot logs. For an explanation on how to enable logging in FaceBot, refer to the 'Logging' section.

If you enable the debug level logging, you should be able to see the JSON sent to Facebook. Here's an example line of log:

16:01:30.478 DEBUG  [http-bio-127.6.222.1-8080-exec-2][FaceBotNetworkAwareBean] Request: {"message":{"text":"This is a bot reply to a user message. The field recipient.id contains the user page-scoped ID."},"recipient":{"id":"123456778975"}}

I'm still looking for a proper way of getting the page-scoped ID. If you know how to do that, please contact me or edit this page.

Once you have instantiated your FaceBotMediator, you will have the following methods to test it:

  • sendMessage(String): simulates a user that sends a message through Facebook Messenger chat to your bot.
  • sendPayload(String): simulates a user that triggers an event that sends a payload back (clicking a button, selecting a quick reply...).
  • forward(MessageEnvelope): simulates an incoming envelope. Useful only for very specific test case.
  • interactiveTest: starts an interactive test. You can send messages or payload using your IDE console. To send a message, just type it. To send a payload, type 'payload:' followed by the payload to send. To stop the simulation, just type 'stop'.
Clone this wiki locally