banetl banetl - 1 year ago 274
reST (reStructuredText) Question

Rest Controller Unit Test

Im trying to do unit tests for a Rest Controller. I did a stub(~mock) for the manager to the database acces and it works well. My only issue is that when I start my unit test it doesn't start the Application.

How can I start the application from my unit test ?

I'm using spring 4.2.3, spring boot 1.3.7, junit 4.12.

Here are my classes :


@SpringApplicationConfiguration(locations = "classpath:/META-INF/spring/mvc/mvc-test-context.xml")
public class RestControllerTest extends AbstractTransitionnalTest {

private IManager Manager;

public void getTestSingleItem(){
Item itm = myTestItemPreInitiallized;;
List<Map> apiResponse = restTemplate.getForObject(networkAddress + "/items", List.class);
// Assertions on apiResponse


class RestController {

private IManager Manager;

// Controller content

Beans in mvc-test-context.xml


Application class that contains the main

@ImportResource({ "classpath:/META-INF/spring/context-application.xml" })
public class Application {

If I run it as it is now the application class isn't started and i get the following erreor : I/O error on GET request for adress:Connection refused

If you don't have the exact solution or would like to propose another way to do this or a workaround, what I wish for is to have the
to be inserted in the
manager instead
class only when I launch my test.

Answer Source

We can use the combination of MockitoJUnitRunner and Spring's MockMvcBuilders class to write the unit test the Spring REST Controller.

I have made changed to your code and refer it below to write the JUnits for your REST Controller.

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import com.fasterxml.jackson.databind.ObjectMapper;

import biz.cogitare.framework.controllers.advices.ExceptionControllerAdvice;

public class RestControllerTest {

    private MockMvc mockMvc;

    private Item item;

    private String itemJSON;

    private Manager manager;

    private RestController restController = new RestController();

    public void setUp() throws Exception {

        mockMvc = MockMvcBuilders.standaloneSetup(restController)
                .setMessageConverters(new MappingJackson2HttpMessageConverter());

         Item item = myTestItemPreInitiallized;

        itemJSON = new ObjectMapper().writeValueAsString(itm);

    public void testQuerySuccess() throws Exception {

        List<Item> items = new ArrayList<>();


                //.andExpect(jsonPath("$[0].id", is(1)))
                //.andExpect(jsonPath("$[0].name", is("xyz")));


    public void testInsertSuccess() throws Exception {



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download