Тест Springboot не возвращает ожидаемый результат - PullRequest
0 голосов
/ 10 мая 2018

все разработчики, моя команда создает микросервис для получения информации о свойствах с помощью Springboot. У нас есть набор тестов, чтобы смоделировать процесс создания запроса Http и проверить ответ, который возвращает информацию о свойстве в классе-заглушке. Вот тест:

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
@AutoConfigureRestDocs(outputDir = "build/asciidoc/generated/snippets")
@ActiveProfiles({ Constants.SPRING_PROFILE_TEST })
public class SwaggerToMarkupTest {
 @Autowired
 private MockMvc mockMvc;
 @Test
public void getProperties() throws Exception {
    // given
    String apiOperationName = "getProperty";

    MockHttpServletRequestBuilder requestBuilder = get("/api/dimension/properties")
            .accept(APPLICATION_JSON_API_VALUE)
            .header(REQUEST_ID_FIELD_NAME, REQUEST_ID_VALUE)
            .header(CLIENT_ID_FIELD_NAME, CLIENT_ID_VALUE)
            .header(CLIENT_VERSION_FIELD_NAME, CLIENT_VERSION_VALUE)
            .header("Authorization", AUTHORIZATION_VALUE);

    // when
    ResultActions actions = mockMvc.perform(requestBuilder);
    actions.andDo(document(apiOperationName, preprocessResponse(prettyPrint())));

    // then
    actions.andExpect(status().isOk());

    String[] files = new File(ASCIIDOC_SNIPPETS_PARENT_FOLDER + "/" + apiOperationName).list();
    assertThat(files).contains("curl-request.adoc", "http-request.adoc", "http-response.adoc");

    String httpResponseDocContent = getHttpResponseDocContent(apiOperationName);
    Assertions.assertThat(httpResponseDocContent).contains("\"type\" : \"properties\"");
}

Вот обработчик запроса:

@GetMapping(value = "/dimension/properties", headers = ACCEPT_APPLICATION_JSON_API_VALUE, produces = APPLICATION_JSON_API_VALUE)
@Timed(absolute = true, name = "timer.rest.getProperty")
public DeferredResult<ResponseEntity<GetPropertyResponseDTO>> getProperty() {
   DeferredResult<ResponseEntity<GetPropertyResponseDTO>> deferredResult = new DeferredResult<>();

   propertyService.getProperty(uuid)
            .map(properties -> {
                ResponseEntity<GetPropertyResponseDTO> responseEntity = new ResponseEntity<>(new GetPropertyResponseDTO(properties),
                        HttpStatus.OK);
                logger.info("Response: " + responseEntity);
                return responseEntity;
            }).subscribe(deferredResult::setResult, deferredResult::setErrorResult);

    return deferredResult;
}

Мы используем Rxjava для асинхронного вызова:

public Observable<List<Property>> getProperty(String uuid) {
    return assetsDomainApi.getAssets(uuid, AssetType.PROPERTY)
        .flatMap(assetsResponseVO -> {
            List<Asset> assets = assetMapper.convert(assetsResponseVO);
            assets = propertyInjector.inject(assets);
            Observable<Property> propObsv =
                Observable.from(assets)
                    .flatMap(assetIdentity -> Observable.just(assetIdentity))
                    .subscribeOn(Schedulers.io())
                    .flatMap(assetIdentity -> {
                        Observable<PropertyDetails> propertyDetailsObs = getPropertyDetails(assetIdentity.getCoreLogicPropertyId());
                        Observable<List<InsuranceProductProperty>> insurancePropertiesObs =
                            getPolicyDetails(assetIdentity);

                        Observable<Property> propertyObservable =
                            Observable.zip(propertyDetailsObs, insurancePropertiesObs,
                                (propDetail, insuranceProperties) -> {
                                    Property property = new Property();
                                    property.setAssetId(assetIdentity.getAssetId());
                                    property.setPropertyDetails(propDetail);
                                    property.setOwnershipType(getOwnershipType(assetIdentity, insuranceProperties.isEmpty()));
                                    if (property.getOwnershipType().equals(DERIVED)) {
                                        property.setSuncorpProducts(new SuncorpProducts<>(insuranceProperties));
                                    }
                                    return property;
                                });
                        return propertyObservable;
                    });
            return propObsv.toList();
        });
}

Проблема в моем тесте, статус возвращается 200, но без тела. Я не уверен, что проблема в mockMvc или это код.

Вот некоторые журналы при запуске этого теста:

2018-05-10 11:59:57,329 [Test worker] INFO APP=marketplace-dimension-mobile-
experience-api | ENV=TEST | REQUEST_ID=a | TRACE_ID= | SPAN_ID= | 
CLIENT_ID=b | CLIENT_VERSION=c | UUID= | PASSIVE=true | CUSTOMER_ENV= | 
GP_ENV= | a.c.s.i.m.a.r.r.GetPropertiesResource - Request - uuid: 123456

2018-05-10 11:59:57,330 [Test worker] INFO APP=marketplace-dimension-mobile-
experience-api | ENV=TEST | REQUEST_ID=a | TRACE_ID= | SPAN_ID= | 
CLIENT_ID=b | CLIENT_VERSION=c | UUID= | PASSIVE=true | CUSTOMER_ENV= | 
GP_ENV= | a.c.s.i.m.a.r.r.GetPropertiesResource - Response: <200 
OK,GetPropertyResponseDTO{data=[ResponseData{type=properties, id=2222, 
attributes=Property{assetid=2222, 
ownershipType=OwnershipType{value=isDerived},  
propertyDetails=PropertyDetails{propertyId=3333, occupancyType=House, 
propertyType=Land, propertySubType=Land: Res House, ...}}]},{}>

Что я пробовал:

  1. Сделайте тот же запрос с тем же заголовком от почтальона с запущенной службой, результат будет иметь полное тело.
  2. Попробуйте отладить из теста, я не могу отладить его должным образом, так как довольно много асинхронных вызовов.

Заранее спасибо

1 Ответ

0 голосов
/ 11 мая 2018

После 3 дней исследования и попытки, наконец, найти ответ, разница в вызове MockMvc. Вместо синхронного вызова следует использовать асинхронный вызов:

mockMvc.perform(asyncDispatch(mockMvc.perform(requestBuilder).andReturn()));

Трудно выполнить отладку, поскольку вывод ошибки не всегда поддерживается каждый раз при запуске.

Спасибо

...