1

I just created a simple integration test with MockMvc for a controller. All works well, but there is no response provided even the controller method returns something. Here is the Controller:

import depmgmt.service.deposit.HouseService;
import depmgmt.service.dto.deposit.HouseDTO;
import depmgmt.serviceweb.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/rest/house")
public class HouseRestController extends BaseController {


private HouseService houseService;

@Autowired
public HouseRestController(HouseService houseService) {
    this.houseService = houseService;
}

@RequestMapping(value = "/", method = RequestMethod.GET)
public List<HouseDTO> getHouses(@RequestParam(value = "page", defaultValue = "1") int page, 
@RequestParam(value = "size", defaultValue = "50") int size) {
    validatePageRequest(page, size);
    return houseService.getHouseList(page, size);
}
}

Here is the test controller:

import com.fasterxml.jackson.databind.ObjectMapper;
import depmgmt.service.deposit.HouseService;
import depmgmt.service.dto.deposit.HouseDTO;
import depmgmt.serviceweb.config.SpringServiceWebConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

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

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {SpringServiceWebConfig.class})
@WebMvcTest(controllers = HouseRestController.class)
public class HouseRestControllerTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private ObjectMapper objectMapper;

@MockBean
private HouseService houseService;


@Test
public void shouldGetHouses() throws Exception {
    //given
    List<HouseDTO> houseDTOS = new ArrayList<>();
    HouseDTO houseDTO = HouseDTO.builder().country(null).city("City").owner(null).name("MyHouse").id(1l).address("HouseAddress").build();
    houseDTOS.add(houseDTO);
    when(houseService.getHouseList(1, 1)).thenReturn(houseDTOS);
    //when
    MvcResult mvcResult = mockMvc.perform(get("/rest/house/")
            .contentType("application/json")
            .param("page", "1")
            .param("size", "1")).andReturn();

    System.out.println(mvcResult.getResponse().getContentAsString());
}
}

When I execute the test the controller method is called successfully and it returns what it should:

        HouseDTO houseDTO = HouseDTO.builder().country(null).city("City").owner(null).name("MyHouse").id(1l).address("HouseAddress").build();
    houseDTOS.add(houseDTO);
    when(houseService.getHouseList(1, 1)).thenReturn(houseDTOS);

enter image description here

However in the test Controller the: mvcResult.getResponse().getContentAsString() returns empty string:

What is wrong in the test?

Meier
  • 3,858
  • 1
  • 17
  • 46
DanutClapa
  • 592
  • 2
  • 7
  • 23
  • Did you test it outside of the test-scope for example using Postman? Doet that work? – Martin van Wingerden Sep 17 '19 at 12:10
  • Nope, I did not deployed and tested as standalone app. I write the unit tests and integration tests first and then I deploy the app and into a web container. Try to assure that the functionality is as expected and not try to figure it out why it does not work in live running when deployed. – DanutClapa Sep 17 '19 at 12:17
  • Then it will be hard to help you. The code looks fine, I could image that something is wrong when mapping using the ObjectMapper to convert the list to a json-string. I tested some similar piece of code and it just works, could you validate that the objectMapper is allright? – Martin van Wingerden Sep 17 '19 at 12:27
  • ok, ill look into ObjectMapper to see if all is ok – DanutClapa Sep 17 '19 at 12:32
  • Ok, solved! So, shame on me!. I was convinced that I have RestController on my Class .... So I did not have either RestController on the Class or ResponseBody on the method level. – DanutClapa Sep 24 '19 at 18:30

1 Answers1

1

The mistake what that there is no @ResponseBody on the method. So either @RestController on the Class or @Controller on the class and @ResponseBody on the method.

DanutClapa
  • 592
  • 2
  • 7
  • 23