Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to UnitTest an Android service that depends on a network connection

I have an Android Service that handles communications with a game sync server by passing messages over TCP back and forth.

I'd like to be able to unit test the behavior of this service. Namely that when data is seen on the wire that data is read, parsed, and sends out the valid corresponding intent and when the Service receives an Intent it correctly creates a message to be sent to the server.

I'm not particularly good about unit testing, but I'm trying to make unit testing part of my practice. I'm not sure how to approach something like this. It feels like somehow I would need to mock up socket and fake the Input and Output streams, but I really don't know how to do that especially as it applies to Android.

Here is the (greatly trimmed down for breverity) service:

public class GameSyncService extends Service {
    Thread mInputThread = new Thread() {

        /**
         * Parse commands from a message string, broadcast the command intents, and
         * return the remainder of the message
         * @param message The message to parse for commands
         * @returns the remaining characters
         */
        private String parseCommands(String message) {
            // Parse the command, Broadcast the Intent and return any remainder
        }

        @Override
        public void run() {
            String message = "";
            int charsRead = 0;
            char [] buffer = new char[BUFFER_SIZE];
            while(!Thread.interrupted()) {
                try {
                    while ((charsRead = mIn.read(buffer)) != -1) {
                        message += new String(buffer).substring(0, charsRead);
                        message = parseCommands(message);
                    }
                } catch (IOException e) {
                    Log.d(LOG_TAG, "Error receiving response: " + e.getLocalizedMessage());
                    disconnectFromServer();
                    connectToServer();
                }
            }
        }
    };

    private BroadcastReceiver mMessageSender = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String message = intent.getStringExtra("message");
            sendMessage(message);
        }

    };

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    private void sendMessage(String message) {
        new SendCommandMessageTask().execute(message);
    }

    /**
     * Create a new connection to the server
     */
    private void connectToServer() {
        try {
            if (mSocket == null) {
                mSocket = new Socket(mHost, mPort);
                mOut = new PrintWriter(mSocket.getOutputStream());
                mIn = new BufferedReader(new InputStreamReader(mSocket.getInputStream()), BUFFER_SIZE);
                sendMessage("Handshake:|" + pInfo.versionName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Disconnect from the server and reset the socket to null
     */
    private void disconnectFromServer() {
        if (mSocket != null) {
            try {
                mIn.close();
                mOut.close();
                mSocket.close();
                mSocket = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public int onStartCommand(Intent i, int flags, int startId) {
        Log.d(LOG_TAG, "GameSyncService Started");
        mHost = i.getStringExtra("host");
        mPort = i.getIntExtra("port", 9000);
        connectToServer();
        mInputThread.start();
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        registerReceiver(mMessageSender, new IntentFilter(COMMAND_MESSAGE_SEND_ACTION));
        try {
            pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(mMessageSender);
        super.onDestroy();
    }
}
like image 393
John Avatar asked Oct 23 '22 23:10

John


1 Answers

Welcome to the world of mocking. What you need to do can be easily done with the help of Android Mock. You should read up on how Expectations work with Android Mock in the project's Writing Tests using Android Mock wiki page.

What I would do is implement a Socket service that encapsulates the underlying TCP/socket calls. Then using Android Mock, you mock your Socket service and using Expectations to verify the correct data is passed in by a higher level method (like GameSyncService )

like image 166
Jason Avatar answered Oct 27 '22 09:10

Jason