### Question:
MessageBrokerClient extends Client { public void subscribeToTopic(String topicName) throws EtiqetException { getTransport().subscribeToTopic( Optional.of(topicName), message -> addMessageToGroup(messagesByTopic, topicName, message) ); } MessageBrokerClient(String clientConfig);  MessageBrokerClient(String primaryClientConfig, String secondaryClientConfig);  MessageBrokerClient(String primaryClientConfig, String secondaryClientConfig, ProtocolConfig protocol); @Override boolean isLoggedOn(); void subscribeToTopic(String topicName); void subscribeToQueue(String queueName); void sendMessageToTopic(Cdr message, Optional<String> topicName); void sendMessageToQueue(Cdr message, String queueName); Cdr waitForMsgOnTopic(Optional<String> topic, int milliseconds); Cdr waitForMsgOnQueue(String queueName, int milliseconds); List<Cdr> getReceivedMessagesFromTopic(String topicName); List<Cdr> getReceivedMessagesFromQueue(String queueName); Optional<Cdr> getLastMessageFromTopic(String topicName); Optional<Cdr> getLastMessageFromQueue(String queueName); void clearQueue(String queueName); @Override BrokerTransport getTransport();  }### Answer:
@Test public void testSubscribeToTopic() throws Exception { final String topicName = "topic"; doAnswer( call -> { Consumer listener = call.getArgument(1); listener.accept(aCdr("NONE").build()); listener.accept(aCdr("NONE").build()); return null; } ).when(transport).subscribeToTopic(eq(Optional.of(topicName)), any(Consumer.class)); messageBrokerClient.subscribeToTopic(topicName); assertEquals(2, messageBrokerClient.getReceivedMessagesFromTopic(topicName).size()); }
@Test public void getReceivedMessagesFromTopic_failOnNullMessage() throws Exception { final String topicName = "topic"; doAnswer( call -> { Consumer listener = call.getArgument(1); listener.accept(aCdr("NONE").build()); listener.accept(null); return null; } ).when(transport).subscribeToTopic(eq(Optional.of(topicName)), any(Consumer.class)); try { messageBrokerClient.subscribeToTopic(topicName); fail("Should have failed after after receiving null message"); } catch (EtiqetRuntimeException e){} }