Зачем получать пустой ответ при написании UnitTest - PullRequest
0 голосов
/ 26 апреля 2020

Я учусь писать модульный тест для SpringBoot Restcontroller, написал это и проходит тестирование

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {FhirApp.class, TestSecurityConfiguration.class})
@AutoConfigureMockMvc
public class ObservationControllerTest {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ObservationService observationService;

    @Test
    public void createObservationResource() throws Exception {

        given(observationService.createObservation(ResourceStringProvider.observationsource()))
                .willReturn(responseDocument);

        String jsonString = objectMapper.writeValueAsString(
                          ResourceStringProvider.observationsource());

        mockMvc.perform(post("/Observation")
            .contentType(MediaType.APPLICATION_JSON)
            .content(jsonString))
            .andExpect(status()
            .isOk());
}

Но как это и это , я также получаю пустой ответ для response.getContentAsString ():

Mockito.when(observationService.createObservation(Mockito.any())).thenReturn(responseDocument);

String jsonString = objectMapper.writeValueAsString(ResourceStringProvider.observationsource());

MockHttpServletResponse response = mockMvc.perform(post("/Observation")
            .contentType(MediaType.APPLICATION_JSON)
            .content(jsonString))
            .andReturn()
            .getResponse();

    assertThat(response.getContentAsString())
        .isEqualTo(new ObjectMapper()
                            .writeValueAsString(responseDocument));

Я уже испробовал предоставленные ими решения:

1: Using Mockito.any(String.class) 
2: webEnvironment = SpringBootTest.WebEnvironment.MOCK
3: using thenCallRealMethod instead of thenReturn(responseDocument)

Но, к сожалению, это не сработало, уже пробовал разные возможности, я также попытался использовать MockitoJunitRunner:

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = {FhirApp.class, TestSecurityConfiguration.class})
@AutoConfigureMockMvc
public class ObservationControllerTest {

private MockMvc mockMvc;

@Mock
private ObservationService observationService;

@Mock
private RequestFilter requestFilter;

@InjectMocks
private ObservationController observationController;

@Before
public void setup() {
    Resource resource = Utility.convertFromStringToFhirResource(Observation.class,ResourceStringProvider.observationResponse());
    responseDocument=Utility.convertFromFhirResourceToMongoInsertibleDoc(resource);

    //these line enabled for MockitoJUnitRunner only 
    this.mockMvc = MockMvcBuilders.standaloneSetup(observationController)
        .setControllerAdvice(new FhirRuntimeException("Error Happened"))
        .addFilters(requestFilter)
        .build();
}

@Test
public void createObservationResource()throws Exception{
    Mockito.when(observationService.createObservation(ResourceStringProvider.observationsource())).thenReturn(responseDocument);

    String jsonString = objectMapper.writeValueAsString(ResourceStringProvider.observationsource());
    MockHttpServletResponse response = mockMvc.perform(
        post("/Observation")
        .contentType(MediaType.APPLICATION_JSON)
        .content(jsonString))
        .andReturn()
        .getResponse();

    assertThat(response.getContentAsString()).isEqualTo(new ObjectMapper().writeValueAsString(responseDocument));
}

Я думаю, что поскольку не многие из них сталкивались с такими проблемами, о проблеме не так много говорят. Что может быть причиной пустого ответа, когда статус ответа в порядке?

Код контроллера:

@RestController
@RequestMapping("/api")
public class ObservationController {

    @Autowired
    private ObservationService observationService;


    @GetMapping("/Observation/{id}")
    public ResponseEntity<Document> getObservationByID(
        @RequestParam("_pretty") Optional<String> pretty,
        @PathVariable("id") String id) {
        Document resultDoc = observationService.getObservationById(id);
        return new ResponseEntity<>(resultDoc, HttpStatus.OK);
    }


    @PostMapping(path = "/Observation", consumes = {"application/json", "application/fhir+json"},
        produces = {"application/json", "application/fhir+json"})
    public ResponseEntity<Document> createObservationResource(@RequestBody String fhirResource) {
        Document fhirDoc = observationService.createObservation(fhirResource);
        return new ResponseEntity<>(fhirDoc,
            Utility.createHeaders(fhirDoc),
            HttpStatus.CREATED);
    }

    //other methods
}

Я понял, что вызов для публикации в тесте должен быть / api / Observation, но это не так не имеет никакого значения. Заранее спасибо.

1 Ответ

0 голосов
/ 27 апреля 2020

Публикация полного ответа вместе с импортом здесь, чтобы он кому-то помог (на самом деле мы не нуждаемся в каких-либо комментариях выше, и это один из способов его запустить, во время моих выводов я наткнулся на эту запись , которая помогает в решении проблемы дизайна, которая может возникнуть, если вы go впереди с подходом @InjectMocks)

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

import com.comitemd.emr.datalayer.fhir.service.ObservationService;
import com.comitemd.emr.datalayer.fhir.utility.ResourceStringProvider;
import com.comitemd.emr.datalayer.fhir.utility.Utility;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bson.Document;
import org.hl7.fhir.r4.model.Observation;
import org.hl7.fhir.r4.model.Resource;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

//@RunWith(MockitoJUnitRunner.class)
public class ObservationControllerStandaloneTest {

    private MockMvc mockMvc;
    @Mock
    private ObservationService observationService;
    @InjectMocks
    private ObservationController observationController;

    private Document responseDocument;
    @Before
    public void setup() {
       MockitoAnnotations.initMocks(this);// enable this or MockitoJUnitRunner.class
        Resource resource = Utility
            .convertFromStringToFhirResource(Observation.class, ResourceStringProvider.observationResponse());
         responseDocument=Utility.convertFromFhirResourceToMongoInsertibleDoc(resource);
        this.mockMvc = MockMvcBuilders.standaloneSetup(observationController).build();
    }

    @Test
    public void createObservationResource()throws Exception{
        Mockito.when(observationService.createObservation(ResourceStringProvider.observationsource()))
            .thenReturn(responseDocument);

        MockHttpServletResponse response = mockMvc.perform(
            post("/api/Observation")
                .contentType(MediaType.APPLICATION_JSON)
                .content(ResourceStringProvider.observationsource()))
            .andDo(MockMvcResultHandlers.print())
            .andReturn()
            .getResponse();

        verify(observationService, times(1)).createObservation(Mockito.any());
        assertThat(response.getContentAsString()).isEqualTo(new ObjectMapper().writeValueAsString(responseDocument));
    }
}
...