### Question:
JavaMailJMSStatistics extends NotificationBroadcasterSupport implements JavaMailJMSStatisticsLocal, DynamicMBean { public void onSuccess(MimeMessage mimeMessage, Address[] addresses) { MessageAndAddresses maa = new MessageAndAddresses(mimeMessage, addresses, null); successCounter.incrementAndGet(); lastSuccessMessage = maa; sendNotification(maa); }  @PostConstruct void registerInJMX(); @PreDestroy void unregisterFromJMX(); void onSuccess(MimeMessage mimeMessage, Address[] addresses); void onFailure(MimeMessage mimeMessage, Address[] addresses, Exception ex); @Override AttributeList getAttributes(String[] attributeNames); @Override Object getAttribute(String attribute); @Override void setAttribute(Attribute attribute); @Override AttributeList setAttributes(AttributeList attributes); @Override Object invoke(String actionName, Object[] params, String[] signature); @Override MBeanInfo getMBeanInfo(); static final String NOTIFICATION_TYPE_SUCCESS; static final String JAVA_MAIL_SEND_FAILURE; static final ObjectName JMX_OBJECT_NAME; }### Answer:
@Test public void testOnSuccess() throws Exception { javaMailJMSStatistics.onSuccess(mimeMessage, addressesTo); assertEquals(1L, getMbeanAttribute("countSuccessful")); }
@Test public void testJmxListener() throws Exception { final StringBuilder result = new StringBuilder(); NotificationListener listener = new NotificationListener() { @Override public void handleNotification(Notification notification, Object handback) { result.append(notification.getType()); } }; MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); platformMBeanServer.addNotificationListener(JavaMailJMSStatistics.JMX_OBJECT_NAME, listener, null, this); javaMailJMSStatistics.onSuccess(mimeMessage, addressesTo); platformMBeanServer.removeNotificationListener(JavaMailJMSStatistics.JMX_OBJECT_NAME, listener); assertEquals(JavaMailJMSStatistics.NOTIFICATION_TYPE_SUCCESS, result.toString()); }