Search code examples
javajunitsmack

Use of Java Smack 4.3.4 in a JUnit Testcase in Maven


I am working on a Java library with some services based on xmpp. For XMPP communication, I use Smack version 4.3.4. The development has so far been without problems and I have also created some test routines that can all be run without errors. After I migrated to a Maven project to generate a FatJar, I wanted to convert the executable test cases into JUnit tests. Unexpectedly, an error occurs, the reason of which I cannot explain. As I said, the code can be run outside of JUnit without any problems.

Below is the simplified test code (establishing a connection to the xmpp server):

@Test
public void connect() 
{
    Builder builder = XMPPTCPConnectionConfiguration.builder();
    builder.setSecurityMode(SecurityMode.disabled);
    builder.setUsernameAndPassword("iec61850client", "iec61850client");
    builder.setPort(5222);      
    builder.setSendPresence(true);          
    try
    {
        builder.setXmppDomain("127.0.0.1");     
        builder.setHostAddress(InetAddress.getByName("127.0.0.1"));
    }
    catch (Exception e)
    {
        e.printStackTrace();            
    }           
    XMPPTCPConnectionConfiguration config = builder.build();        
    XMPPTCPConnection c = new XMPPTCPConnection(config);        
    c.setReplyTimeout(5000);
    try
    {           
        c.connect().login();
    }
    catch (Exception e)
    {
        e.printStackTrace();            
    }           
}

And here is the error message I get:

Exception in thread "Smack Reader (0)" java.lang.AssertionError
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1154)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$1000(XMPPTCPConnection.java:1092)
at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:1112)

In Smack it boils down to this 'assert' instruction:

assert (config.getXMPPServiceDomain().equals(reportedServerDomain));

Any idea what the problem might be or similar problems? I'm grateful for any help!

Thanks a lot, Markus


Solution

  • If you look at the source code you will find that reportedServerDomain is extracted from the server's stream open tag. In this case the xmpp domain reported by the server does not match the one that is configured. This should usually not happen, but I assume it is related to the way you run the unit tests. Or more precisely, related to the remote server or mocked server that is used in the tests. If you enable smack's debug output, you will see the stream open tag and the 'from' attribute and its value. Compare this with the configured XMPP service domain in the ConnectionConfiguration.