Java Springboot测试未返回预期结果

Java Springboot测试未返回预期结果,java,spring,spring-boot,rx-java,mockmvc,Java,Spring,Spring Boot,Rx Java,Mockmvc,所有开发人员,我的团队正在创建一个微服务来使用springboot获取属性信息。我们有一个测试套件来模拟发出Http请求的过程,并验证在存根类中返回属性信息的响应。以下是测试: @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @AutoConfigureMockMvc @AutoConfigureRestDocs(outputDir

所有开发人员,我的团队正在创建一个微服务来使用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();
        });
}
mockMvc.perform(asyncDispatch(mockMvc.perform(requestBuilder).andReturn()));
问题是在我的测试中,状态返回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, ...}}]},{}>
我所尝试的:

在服务运行时,从postman发出具有相同标头的相同请求,结果具有完整的正文。 尝试从测试中调试,我无法正确调试它,因为有很多异步调用。
提前感谢

经过3天的研究和尝试,终于找到了答案,区别在于MockMvc调用。与其使用同步调用,不如使用异步调用:

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();
        });
}
mockMvc.perform(asyncDispatch(mockMvc.perform(requestBuilder).andReturn()));
由于每次运行时失败的输出都不一致,因此很难调试

谢谢