Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: how to unit test interaction with the socket based on Robolectric? #146

commjoen opened this issue May 12, 2015 · 5 comments


Copy link

@commjoen commjoen commented May 12, 2015


we are currently using both Espresso 2 and Robolectric based tests. Now I was wondering: is there a way to mock a socket to bind on in order to create some test events?

Copy link

@nkzawa nkzawa commented May 12, 2015

Sorry, I'm not familiar with Robolectric. On the unittest of the library, we just spin up a server and test against it.

Copy link

@icapurro icapurro commented May 18, 2015

I'm also interested in a way to mock the server without the need of having a local instance of the server running.

I'm using this library to mock my HTTP server

I would like to be able to do something similar. Like

sServer = new SocketServerMock("/myScope");
sServer.start(); // maybe this will trigger the connect event in the client
// test something and assert an action triggered by an event
sServer.emit(eventName, eventPayload);
// boom, the action got triggered.
sServer.stop(); // this could trigger the disconnect

Looking at the code my first impression on how to do this is to extend the Manager class and provide the functionality to always be connected and a method to emit an event, then in the Socket or Options class set this Manager to be used. Do you think this is a good approach? if so I might start working on something.

Copy link

@icapurro icapurro commented May 18, 2015

Some of the private definitions in the classes did not allowed me to successfully create a stub of the Manager without rewriting a lot of code, but in the meantime I just used the Emitter class as my socket object.

import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.Socket;
import org.json.JSONException;
import org.json.JSONObject;

public class SocketServerStub {

    protected Emitter socket = new Emitter();

    public SocketServerStub() {}

    public Emitter getSocket() {
        return socket;

    public void start() {

    public void emit(String event, String jsonString) throws JSONException {
        JSONObject obj = new JSONObject(jsonString);
        socket.emit(event, obj);

    public void stop() {

It supports the main functionality I wanted.

Copy link

@commjoen commjoen commented May 28, 2015

The idea of having such an emitter is quite nice. Just to make sure: how can we make sure that the mockwebserver is then the one to bind our sockets to?

Copy link

@icapurro icapurro commented Jun 8, 2015

I ended up doing something a bit different, I defined these two classes and

Then after creating the SocketServerStub object I pass the socket object into my singleton class in charge of managing the socket connection like this

SocketServerStub socketServer = new SocketServerStub();

and then I just need to run

socketServer.emit("myEvent", "myPayload");

to make sure an event is emitted.

This is a basic stubbing of the socket client, it's intended just to trigger actions inside the app that will only get triggered with a socket event.

@commjoen commjoen closed this as completed Aug 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants