Skip to content

Mock device communications

For testing purposes, you can mock device communication.

To mock a device you have two choices:

  1. Mock at communication protocol level, using a MockProtocol
  2. Mock at client level, using a MockClient

1) Using a mock protocol

Using a Mock protocol produces something similar to what you have with an IoTize device.

Using a pre-implemented mock protocol

To do


MockProtocol moclProtocol = new MockProtocol();
// Map a specific response to a command
mockProtocol.mapResponse(Command.GET("/1/2/3"), Response.SUCCESS("Hello World".getBytes()));
// Command GET /1/2/3 will now always return a successful response with "Hello World" payload

// Map a dynamic response to a command: 
mockProtocol.mapResponse(Command.GET("/1/2/3"), new ResponseAdapter<byte[]>() {
            @Override
            public Response<byte[]> adapt(Command request) {
                Response<byte[]> response = // your custom response here...
                return response;
            }
        });


// Map a response with a custom filter
mockProtocol.mapResponse(new Filter<Command>() {
             @Override
             public boolean isFiltered(Command command) {
                 boolean isFiltered = // return false if you want to apply the ResponseAdapter to this command
                 return isFiltered;
             }
         }
         , new ResponseAdapter<byte[]>() {
            @Override
            public Response<byte[]> adapt(Command request) {
                Response<byte[]> response = // your custom response here...
                return response;
            }
        });

Pre-defined mock protocol

This Mirror protocol always returns a successful response. The payload of the response is the encoded command.

let protocol: MockProtocol = MockProtocol.MIRROR();
ComProtocol protocol = MockProtocol.MIRROR();

See javadoc for Android of MockProtocol