title: How to mock and write Unit testing for Azure Service Bus Topic using Junit and Java?
body: <p>I have a method to send and receive a messages from Azure service Bus Topic, I need to mock and write test cases for that method using Junit 5.</p>
  <p>Sample code based on Microsoft documentation</p>
  <pre><code>    public class MyServiceBusTopicClient {
  
      static final Gson GSON = new Gson();
      
      public static void main(String[] args) throws Exception, ServiceBusException {
          // TODO Auto-generated method stub
  
          TopicClient sendClient;
          String connectionString = &quot;Endpoint=sb://&lt;NameOfServiceBusNamespace&gt;.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=&lt;AccessKey&gt;&quot;;
          sendClient = new TopicClient(new ConnectionStringBuilder(connectionString, &quot;BasicTopic&quot;));       
          sendMessagesAsync(sendClient).thenRunAsync(() -&gt; sendClient.closeAsync());
      }
  
      static CompletableFuture&lt;Void&gt; sendMessagesAsync(TopicClient sendClient) {
          List&lt;HashMap&lt;String, String&gt;&gt; data =
                  GSON.fromJson(
                          &quot;[&quot; +
                                  &quot;{'name' = 'Einstein', 'firstName' = 'Albert'},&quot; +
                                  &quot;{'name' = 'Heisenberg', 'firstName' = 'Werner'},&quot; +
                                  &quot;{'name' = 'Curie', 'firstName' = 'Marie'},&quot; +
                                  &quot;{'name' = 'Hawking', 'firstName' = 'Steven'},&quot; +
                                  &quot;{'name' = 'Newton', 'firstName' = 'Isaac'},&quot; +
                                  &quot;{'name' = 'Bohr', 'firstName' = 'Niels'},&quot; +
                                  &quot;{'name' = 'Faraday', 'firstName' = 'Michael'},&quot; +
                                  &quot;{'name' = 'Galilei', 'firstName' = 'Galileo'},&quot; +
                                  &quot;{'name' = 'Kepler', 'firstName' = 'Johannes'},&quot; +
                                  &quot;{'name' = 'Kopernikus', 'firstName' = 'Nikolaus'}&quot; +
                                  &quot;]&quot;,
                          new TypeToken&lt;List&lt;HashMap&lt;String, String&gt;&gt;&gt;() {
                          }.getType());
  
          List&lt;CompletableFuture&gt; tasks = new ArrayList&lt;&gt;();
          for (int i = 0; i &lt; data.size(); i++) {
              final String messageId = Integer.toString(i);
              Message message = new Message(GSON.toJson(data.get(i), Map.class).getBytes(UTF_8));
              message.setContentType(&quot;application/json&quot;);
              message.setLabel(&quot;Scientist&quot;);
              message.setMessageId(messageId);
              message.setTimeToLive(Duration.ofMinutes(2));
              System.out.printf(&quot;Message sending: Id = %s\n&quot;, message.getMessageId());
              tasks.add(
                      sendClient.sendAsync(message).thenRunAsync(() -&gt; {
                          System.out.printf(&quot;\tMessage acknowledged: Id = %s\n&quot;, message.getMessageId());
                      }));
          }
          return CompletableFuture.allOf(tasks.toArray(new CompletableFuture&lt;?&gt;[tasks.size()]));
      }
  }
  </code></pre>
  <p>Method used to receive messages from the azure service bus subscription.</p>
  <pre><code>import com.microsoft.azure.servicebus.*;
  import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
  import com.microsoft.azure.servicebus.primitives.ServiceBusException;
  import com.google.gson.Gson;
  import static java.nio.charset.StandardCharsets.*;
  import java.time.Duration;
  import java.util.*;
  import java.util.concurrent.*;
  
  public class MyServiceBusSubscriptionClient {
      static final Gson GSON = new Gson();
      
      public static void main(String[] args) throws Exception, ServiceBusException {
          String connectionString = &quot;Endpoint=sb://&lt;NameOfServiceBusNamespace&gt;.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=&lt;AccessKey&gt;&quot;;
          
          SubscriptionClient subscription1Client = new SubscriptionClient(new ConnectionStringBuilder(connectionString, &quot;BasicTopic/subscriptions/Subscription1&quot;), ReceiveMode.PEEKLOCK);
          SubscriptionClient subscription2Client = new SubscriptionClient(new ConnectionStringBuilder(connectionString, &quot;BasicTopic/subscriptions/Subscription2&quot;), ReceiveMode.PEEKLOCK);
          SubscriptionClient subscription3Client = new SubscriptionClient(new ConnectionStringBuilder(connectionString, &quot;BasicTopic/subscriptions/Subscription3&quot;), ReceiveMode.PEEKLOCK);        
  
          ExecutorService executorService = Executors.newCachedThreadPool();
          registerMessageHandlerOnClient(subscription1Client, executorService);
          registerMessageHandlerOnClient(subscription2Client, executorService);
          registerMessageHandlerOnClient(subscription3Client, executorService);
      }
      
      static void registerMessageHandlerOnClient(SubscriptionClient receiveClient, ExecutorService executorService) throws Exception {
          // register the RegisterMessageHandler callback
          receiveClient.registerMessageHandler(
                  new IMessageHandler() {
                      // callback invoked when the message handler loop has obtained a message
                      public CompletableFuture&lt;Void&gt; onMessageAsync(IMessage message) {
                          // receives message is passed to callback
                          if (message.getLabel() != null &amp;&amp;
                                  message.getContentType() != null &amp;&amp;
                                  message.getLabel().contentEquals(&quot;Scientist&quot;) &amp;&amp;
                                  message.getContentType().contentEquals(&quot;application/json&quot;)) {
  
                              byte[] body = message.getBody();
                              Map scientist = GSON.fromJson(new String(body, UTF_8), Map.class);
  
                              System.out.printf(
                                      &quot;\n\t\t\t\t%s Message received: \n\t\t\t\t\t\tMessageId = %s, \n\t\t\t\t\t\tSequenceNumber = %s, \n\t\t\t\t\t\tEnqueuedTimeUtc = %s,&quot; +
                                              &quot;\n\t\t\t\t\t\tExpiresAtUtc = %s, \n\t\t\t\t\t\tContentType = \&quot;%s\&quot;,  \n\t\t\t\t\t\tContent: [ firstName = %s, name = %s ]\n&quot;,
                                      receiveClient.getEntityPath(),
                                      message.getMessageId(),
                                      message.getSequenceNumber(),
                                      message.getEnqueuedTimeUtc(),
                                      message.getExpiresAtUtc(),
                                      message.getContentType(),
                                      scientist != null ? scientist.get(&quot;firstName&quot;) : &quot;&quot;,
                                      scientist != null ? scientist.get(&quot;name&quot;) : &quot;&quot;);
                          }
                          return receiveClient.completeAsync(message.getLockToken());
                      }
  
                      // callback invoked when the message handler has an exception to report
                      public void notifyException(Throwable throwable, ExceptionPhase exceptionPhase) {
                          System.out.printf(exceptionPhase + &quot;-&quot; + throwable.getMessage());
                      }
                  },
                  // 1 concurrent call, messages are auto-completed, auto-renew duration
                  new MessageHandlerOptions(1, false, Duration.ofMinutes(1)),
                  executorService);
      }
  }
  </code></pre>
  <p>I'm not sure about the exact implementation for mocking the above methods. It would be helpful If I get any documentation for the Implemenation.</p>
  <p>Any one please advise on this ?</p>