title: How to write junit for RabbitListenerEndpointRegistry in spring framework
body: <p>I have rest controller to start/stop rabbitmq QUEUE. That is working fine. But i need to write junit for this rest controller. I have done the junit as well but that does not looks good. I am not sure how better we can write junit for this rest controller.  Please help me to write better junit for this.</p>
  <p>We are using spring framework and mockito in our project.</p>
  <p><strong>My Rest Controller</strong></p>
  <pre class="lang-java prettyprint-override"><code>package com.compnay.depart.rest;
  import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
  import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.RestController;
  @RestController
  public class QueueController {
    
      private static final String LISTENER_ID = &quot;lister&quot;;
      
      @Autowired
      private RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
      
  
      @GetMapping(path = &quot;/&quot;+LISTENER_ID+&quot;/start&quot;)
      public String start() {
          MessageListenerContainer listenerContainer = rabbitListenerEndpointRegistry.getListenerContainer(LISTENER_ID);
          listenerContainer.start();
          return &quot;started&quot;;
      }
  
      @GetMapping(path = &quot;/&quot;+LISTENER_ID+&quot;/stop&quot;)
      public String stop() {
          MessageListenerContainer listenerContainer = rabbitListenerEndpointRegistry.getListenerContainer(LISTENER_ID);
          listenerContainer.stop();
          return &quot;stopped&quot;;
      }
  
      //This getter and setter method, i have created for JUNIT. 
      public RabbitListenerEndpointRegistry getRabbitListenerEndpointRegistry() {
          return rabbitListenerEndpointRegistry;
      }
  
      public void setRabbitListenerEndpointRegistry(RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry) {
          this.rabbitListenerEndpointRegistry = rabbitListenerEndpointRegistry;
      }
      
      
      
  }
  </code></pre>
  <p><strong>This is my JUNIT</strong></p>
  <pre class="lang-java prettyprint-override"><code>package com.compnay.depart.rest;
  import static org.junit.Assert.assertEquals;
  import static org.mockito.ArgumentMatchers.anyString;
  import static org.mockito.Mockito.when;
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.amqp.core.MessageListener;
  import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
  import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
  import org.springframework.amqp.support.converter.MessageConverter;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.mock.mockito.MockBean;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.springframework.test.context.support.AnnotationConfigContextLoader;
  import org.springframework.test.web.client.MockRestServiceServer;
  import com.fasterxml.jackson.databind.ObjectMapper;
  
  
  @RunWith(SpringRunner.class)
  @ContextConfiguration(loader=AnnotationConfigContextLoader.class,
  classes={  ObjectMapper.class})
  public class QueueControllerTest 
   {
  
      private MockRestServiceServer server;
  
      @Autowired
      public TraceabilityRestTemplate restTemplate;
      
      @MockBean
      private RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
      
      public QueueController filingListnerController = new QueueController();
      
      @Before
      public void init() {
          server = MockRestServiceServer.bindTo(restTemplate).build();
      }
  
      @After
      public void shutdown() {
          server.reset();
      }
  
  
      @Test
      public void testStart() {
          when(rabbitListenerEndpointRegistry.getListenerContainer(anyString())).thenReturn(new MessageListenerContainer() {
              
              @Override
              public void stop() {
                  // TODO Auto-generated method stub
                  
              }
              
              @Override
              public void start() {
                  // TODO Auto-generated method stub
                  
              }
              
              @Override
              public boolean isRunning() {
                  // TODO Auto-generated method stub
                  return false;
              }
              
              @Override
              public void setupMessageListener(MessageListener messageListener) {
                  // TODO Auto-generated method stub
                  
              }
              
              @Override
              public MessageConverter getMessageConverter() {
                  // TODO Auto-generated method stub
                  return null;
              }
          });
  
         
          filingListnerController.setRabbitListenerEndpointRegistry(rabbitListenerEndpointRegistry);
          String response = filingListnerController.start();
  
          assertEquals(&quot;started&quot;, response);
      }
      
  
      @Test
      public void testStop() {
          when(rabbitListenerEndpointRegistry.getListenerContainer(anyString())).thenReturn(new MessageListenerContainer() {
              
              @Override
              public void stop() {
                  // TODO Auto-generated method stub
                  
              }
              
              @Override
              public void start() {
                  // TODO Auto-generated method stub
                  
              }
              
              @Override
              public boolean isRunning() {
                  // TODO Auto-generated method stub
                  return false;
              }
              
              @Override
              public void setupMessageListener(MessageListener messageListener) {
                  // TODO Auto-generated method stub
                  
              }
              
              @Override
              public MessageConverter getMessageConverter() {
                  // TODO Auto-generated method stub
                  return null;
              }
          });
  
         
          filingListnerController.setRabbitListenerEndpointRegistry(rabbitListenerEndpointRegistry);
          String response = filingListnerController.stop();
  
          assertEquals(&quot;stopped&quot;, response);
      }   
  }
  </code></pre>