Springboot测试未返回预期结果

时间:2018-05-10 02:29:43

标签: java spring spring-boot rx-java mockmvc

所有开发人员,我的团队正在创建一个微服务,以使用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. 尝试从测试中调试,因为很多异步调用,我无法正确调试它。
  3. 先谢谢

1 个答案:

答案 0 :(得分:1)

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

df_clean <- na.omit(df)
df_clean

   Disposition    Anything Analysis
1       answer  2.5472195        2
2    no answer  1.0740980        1
3     whatever  0.6048274        3
5       answer  0.3179986        2
6    no answer -1.1755824        1
7     whatever  0.9420611        3
9       answer  0.0178733        2
10   no answer -0.0762933        1
11    whatever  0.8310968        3

由于每次运行失败的输出都不一致,因此很难调试。

谢谢