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.
We are using spring framework and mockito in our project.
My Rest Controller
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 = "lister";
@Autowired
private RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
@GetMapping(path = "/"+LISTENER_ID+"/start")
public String start() {
MessageListenerContainer listenerContainer = rabbitListenerEndpointRegistry.getListenerContainer(LISTENER_ID);
listenerContainer.start();
return "started";
}
@GetMapping(path = "/"+LISTENER_ID+"/stop")
public String stop() {
MessageListenerContainer listenerContainer = rabbitListenerEndpointRegistry.getListenerContainer(LISTENER_ID);
listenerContainer.stop();
return "stopped";
}
//This getter and setter method, i have created for JUNIT.
public RabbitListenerEndpointRegistry getRabbitListenerEndpointRegistry() {
return rabbitListenerEndpointRegistry;
}
public void setRabbitListenerEndpointRegistry(RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry) {
this.rabbitListenerEndpointRegistry = rabbitListenerEndpointRegistry;
}
}
This is my JUNIT
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("started", 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("stopped", response);
}
}