JmxWhiteboardSupport { protected synchronized void registerMBean(Object mbean, final ServiceReference props) { log.debug("registerMBean: Adding MBean {}", mbean); ObjectName objectName = getObjectName(props); if (objectName != null || mbean instanceof MBeanRegistration) { MBeanHolder holder = createMBeanHolder(mbean, objectName); if (holder != null) { MBeanServer[] mbeanServers = this.mbeanServers; String[] warnExceptions = getStringPlusProperty(props, "warning.exceptions"); for (MBeanServer mbeanServer : mbeanServers) { holder.register(mbeanServer, warnExceptions); } mbeans.put(mbean, holder); } else { log.error( "registerMBean: Cannot register MBean service {} with MBean servers: Not an instanceof DynamicMBean or not MBean spec compliant standard MBean", mbean); } } else { log.error( "registerMBean: MBean service {} not registered with valid jmx.objectname propety and not implementing MBeanRegistration interface; not registering with MBean servers", mbean); } }    }
@Test public void testRegisterMBean() throws Exception { final Logger ml = Mockito.mock(Logger.class); JmxWhiteboardSupport s = new JmxWhiteboardSupport() { @Override MBeanHolder createMBeanHolder(Object mbean, ObjectName objectName) { MBeanHolder mbh = super.createMBeanHolder(mbean, objectName); setPrivateField(mbh, "log", ml); return mbh; } }; final Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put("jmx.objectname", "test:key=val"); ServiceReference<?> sref = Mockito.mock(ServiceReference.class); Mockito.when(sref.getProperty(Mockito.anyString())).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return props.get(invocation.getArguments()[0].toString()); } }); MBeanServer ms = mockMBeanServer(); s.addMBeanServer(ms); Mockito.verify(ml, Mockito.never()).error(Mockito.anyString(), Mockito.isA(Throwable.class)); Mockito.verify(ml, Mockito.never()).warn(Mockito.anyString(), Mockito.isA(Throwable.class)); s.registerMBean(Mockito.mock(DynamicMBean.class), sref); Mockito.verify(ml, Mockito.never()).error(Mockito.anyString(), Mockito.isA(Throwable.class)); Mockito.verify(ml, Mockito.never()).warn(Mockito.anyString(), Mockito.isA(Throwable.class)); }