I have need to mock an XMPP server for testing purposes. My current intention is to run a local XMPP server and tie in my test code there, but I wonder if anyone is aware of an XMPP mocking library which already exists?
-
1I don't know if this is useful, but on the book [Growing Object Oriented Software, Guided by Tests](http://www.growing-object-oriented-software.com/) the author give an example testing an application which uses XMPP, and they chosed for start an OpenFire server, rathen than mock it. If you can, get a copy as they also talk quite a lot about how to test asyncronous application ... and it's **very**, **very** good. – Augusto Oct 10 '11 at 19:40
3 Answers
To successfully mock an entire XMPP server you'd basically end up with... an XMPP server. I personally think that's the most reliable way to test overall, though I use smaller more specific tests for self-contained pieces of code.
You may find this book chapter interesting, Remko is probably the most test-obsessed person I know, and it was written out of his own recent experience developing a client: http://el-tramo.be/blog/beautiful-xmpp-testing

- 7,924
- 1
- 28
- 33
-
have a read off this. http://martinfowler.com/articles/mocksArentStubs.html especially the part "The Difference Between Mocks and Stubs" its will help you out for sure. definitely clarified a few things in my mind – filthy_wizard Sep 19 '15 at 12:07
This may be somewhat dependent on what tools you are using, but in Smack, for example, you can create your own Connection class which will serve as a mock server. This is done within the test cases for Smack.
Basically you feed the reply IQ or Message packets into the connection and then make your calls via the API. The preset packets are then returned as the results. One thing you do have to take into account is the asynchronous nature of XMPP, and make sure your test server will work the same way.
Other XMPP libs may offer some similar approaches to enable unit testing.
If, on the other hand, you are trying to run full blown integration test scenarios, then I think the local server is the right way to go.

- 24,062
- 5
- 49
- 58
Only Mock types you own - search for this article by Freeman n co..
The idea is that you should only mock roles that you control. In this case, an XMPP server's interface is not one that you control.. So define a Role (interface) that your app needs & which is satisfied by the XMPP server.
You may end up with a simpler interface - e.g. SendTo(user, message) . You then need to write an adapter which fits the role to the XMPP server. This thing will handle all the quirkiness associated with your choice of an XMPPServer.
This simpler interface is much easier to mock and resilient to change. HTH

- 134,492
- 47
- 225
- 308