I often want to test that my MBeans work correctly when accessed remotely. For example it's easy to accidentally introduce non-serializable objects in them. It's a pain to set up a real remote connection, but you can make a loopback connection in the same JVM to test most of the same things. Here's how.

import javax.management.*;
import javax.management.remote.*;


MBeanServer mbs = ...something...;
// for example, java.lang.management.ManagementFactory.getPlatformMBeanServer();

// Make a connector server...
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
JMXConnectorServer cs =
    JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
JMXConnector cc = null;
try {
    JMXServiceURL addr = cs.getAddress();

    // Now make a connector client using the server's address
    cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();

    ...test logic here, using mbsc...
    ...e.g. String attr = (String) mbsc.getAttribute(objectName, "MyAttr");
    ...or   mbsc.addNotificationListener(objectName, listener, filter, null);
} finally {
    if (cc != null)

If you do your tests using mbsc instead ofmbs, then MBean Server operations will go through RMI. Even though the connection never leaves the JVM, this enables you to test that your MBean code is network-friendly.

You'll typically want to stop the connector server before your test exits. Otherwise it will prevent your JVM from exiting promptly. I generally pop this into a try/finally so I'm sure it will get executed even if the test gets an exception.

Obviously you can wrap all this logic into a class that looks like this:

public class JMXRemoteTest {
    public static interface Test {
        public void test(MBeanServerConnection mbsc) throws Exception;

    public static void remoteTest(MBeanServer mbs, Test test) {

and use it like this:

JMXRemoteTester.remoteTest(mbs, new JMXRemoteTester.Test() {
    public void test(MBeanServerConnection mbsc) throws Exception {
        // example test
        mbsc.addNotificationListener(objectName, listener, filter, null);
        mbsc.invoke(objectName, "provokeNotification", null, null);

Or if you have a bunch of remote tests, you can set up the MBeanServerConnection in your setup() method or equivalent, and use it for all the tests, then close the JMXConnector andJMXConnectorServer in the tearDown() method.