powered by Jive Software

Testing plugins

I’‘ve just written a heap of plugins and have been testing them by packaging them up, firing up Openfire and running some XMPP packets through them to see how they respond. However, there must be an easier way to do this that I’'ve missed.

Is it possible to run JUnit tests on a plugin or run it in some sort of test mode?

What I usually do is wrapping Smack in a JUnit test to perform a black-box like test. Smacks works for me in most tests, as it’'s quite easily adaptable.

Thanks for the reply.

Do you use Smack in JUnit tests to test your Openfire plugins or to test your smack clients?

Message was edited by: leonroy

I’'m using it to test the plugins.

A typical plugin (at least, ours are typically like this) perform an action based on a particular IQ. It’'s quite easy to create a similar IQ packet representation in Smack, open up a Smack connection to Openfire, send the request and collect the answer.

This example uses JUnit 4 to see if sending an IQ packet without a child element to a particular component returns an error:

public void testGetNoChildElement()
{    // create IQ-get without child element.
  final IQ request = new IQ() {
    public String getChildElementXML() {
      return "";
  request.setTo("sub." + XMPP_DOMAIN);
  request.setType(IQ.Type.GET);   // create packet collector that waits for the response (same packet id)
  final PacketCollector col = conn.createPacketCollector(new PacketIDFilter(request.getPacketID()));   // send request
  conn.sendPacket(request);   // start waiting for the response
  final Packet result = col.nextResult(WAIT_FOR_RESULT_IN_MILLIS);   // if the result is null, no response was received.
  assertNotNull("Every IQ SET (or GET) stanza should be replied to!", result);   // validate response
  assertTrue("Were expecting an IQ stanza, nothing else.", result instanceof IQ);
  final XMPPError error =((IQ) result).getError();
  assertNotNull("The error);
  assertEquals(XMPPError.Type.MODIFY, error.getType());
  // the comparison based on #toString() is a hack, as Condition is not an enum.
  assertEquals(XMPPError.Condition.bad_request.toString(), error.getCondition().toString());

Many thanks for that, it looks like what I need!