### Question:
MessagePublisher implements AutoCloseable { synchronized void publish(Message message) throws Exception { LoggingService.logDebug(MODULE_NAME, "Start publish message :" + this.name ); byte[] bytes = message.getBytes(); try { archive.save(bytes, message.getTimestamp()); } catch (Exception e) { logError(MODULE_NAME, "Message Publisher (" + this.name + ")unable to archive message", new AgentSystemException(e.getMessage(), e)); } for (MessageProducer producer: producers) { try { TextMessage msg = MessageBusServer.createMessage(message.toJson().toString()); producer.send(msg, DeliveryMode.NON_PERSISTENT, javax.jms.Message.DEFAULT_PRIORITY, javax.jms.Message.DEFAULT_TIME_TO_LIVE); } catch (Exception e) { logError(MODULE_NAME, "Message Publisher (" + this.name + ") unable to send message", new AgentSystemException(e.getMessage(), e)); } } LoggingService.logDebug(MODULE_NAME, "Finished publish message : " + this.name); } MessagePublisher(String name, Route route, List<MessageProducer> producers); String getName(); synchronized void close(); synchronized List<Message> messageQuery(long from, long to); Route getRoute();  }### Answer:
@Test public void testPublishWhenMessageIsArchived() { try { messagePublisher.publish(message); Mockito.verify(messageArchive, atLeastOnce()).save(any(byte[].class), anyLong()); verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, "Start publish message :name"); verifyStatic(LoggingService.class); LoggingService.logDebug(MODULE_NAME, "Finished publish message : name"); } catch (Exception e) { fail("This should not happen"); } }
@Test public void throwExceptionWhenArchiveMessageSaveIsCalled() { try { PowerMockito.doThrow(mock(Exception.class)).when(messageArchive).save(Mockito.any(byte[].class), anyLong()); messagePublisher.publish(message); Mockito.verify(messageArchive, atLeastOnce()).save(any(byte[].class), anyLong()); verifyStatic(LoggingService.class); LoggingService.logError(eq(MODULE_NAME), eq("Message Publisher (name)unable to archive message"), any()); } catch (Exception e) { fail("This should not happen"); } }