public static void register(String name, Object instance) {
    try {
      Class<Object> mbeanInterface = guessMBeanInterface(instance);
      ManagementFactory.getPlatformMBeanServer().registerMBean(new StandardMBean(instance, mbeanInterface), new ObjectName(name));

    } catch (MalformedObjectNameException | NotCompliantMBeanException | InstanceAlreadyExistsException | MBeanRegistrationException e) {
      throw new IllegalStateException("Can not register MBean [" + name + "]", e);
    }
  }
@Test
  public void register_fails_if_mbean_interface_can_not_be_found() {
    assertThatThrownBy(() -> Jmx.register(FAKE_NAME, "not a mbean"))
      .isInstanceOf(IllegalArgumentException.class)
      .hasMessage("Can not find the MBean interface of class java.lang.String");
  }