开发者

How to mock a "server - android device" communication

开发者 https://www.devze.com 2023-03-14 14:30 出处:网络
I have an application on Android that communicates with a server through a socket. I have also implemented a protocol buffer type of messages to get more info on the communication.

I have an application on Android that communicates with a server through a socket. I have also implemented a protocol buffer type of messages to get more info on the communication.

I have yet to implement on the server all the logic to respond to the android client requests. Because of this I can't test on the client what happens when I send a request.. What could I use to help me test this? Is there a (simple) way of creating a mock server, for simulating responses?

I was told to read something about Junit and also something here :http://developer.android.com/guide/topics/testing/testing_android.html but not sure it's the right path to go..

Thank you

edit1

Sorry was on a hurry when wrote the topic. Thanks for all your answers but my situation is a bit different I think. Communication protocol is TCP, and the messages are of type "protocol buffers" not XML or JSON. So Maque looked like a开发者_Python百科 cool thing, but not doable here. Also the server side is developed on C#, not Java.

Thank you once again


If your server uses Java too (Servlets/JSP) you could share the message processing logic and define some unit tests on it.


We've had to solve similar problems in our development. You have several choices - which makes sense depends upon your situation and what, exactly, you want to test.

In some of our tests we make use of a very simple HTTP server (our protocol runs on top of HTTP) running on the host machine, which is configured with canned responses to certain requests. We wrote it using Python's SimpleHTTPServer class. The benefit of this approach is that you're doing a full end-to-end test, including the Android network stack. The disadvantage is that it can be fiddly to setup (we have a test-harness that runs on the development machine, starts up the server, runs the tests on an emulator or device and collects the results).

If you want to mock out the low-level code on the device itself, look at Borachio for mocking and RoboGuice for dependency injection. There's a full worked example of how to do this here.

Finally, you might be able to use Robolectric and run your tests on your development machine. The downside is that your tests will not be running on the Android device itself, and it can be difficult to test the interaction between your code and the OS effectively.


If you are using HTTP as your socket/server protocol then you may want to check out a new application called Maque. It's designed to help test and develop HTTP and data driven applications. It has a built in HTTP 1.1 server and provides a UI to allow you to create your API and test requests without having to code or deploy a server stack.

If you are running automated tests like Paul, then you may face some of same challenges of starting the app up and then running the app then shutting it down, but for development and regular testing it should handle what you are looking for.

You can get the cross-platform (OS X, Windows, Linux) beta here: http://maqueapp.com/getBeta.html


Check out Square's MockWebServer, part of OkHttp now.

0

精彩评论

暂无评论...
验证码 换一张
取 消