这是我为JUnit测试编写的用于正面和负面测试的代码。

@Test
public void getMaintenenceIntervalsByMetadataOKTest() throws Exception {

    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
          params.set("vinModelYear", "2016");
          params.set("vinModelCode", "1633F6");
          params.set("vinEngineCode", "CZTA");
          params.set("interval", "100000");
          params.set("vinTransmissionCode", "");
          params.set("importerNumber", "");
          params.set("makeCode", "V");
          params.set("descriptionText", "");
          params.set("languageCode", "en-US");
          params.set("dealerCode", "408083");

    mvc.perform(get("/upg-admin-controller/maintenence-intervals-by-metadata")
            .contentType(MediaType.APPLICATION_JSON)
            .params(params))
            .andExpect(status().isAccepted());
}

@Test
public void getMaintenenceIntervalsByMetadata400Test()
        throws Exception {

    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
          params.set("vinModelYear", "2000");
          params.set("vinModelCode", "8727R9");
          params.set("vinEngineCode", "GTAV");
          params.set("interval", "100000");
          params.set("vinTransmissionCode", "");
          params.set("importerNumber", "");
          params.set("makeCode", "T");
          params.set("descriptionText", "");
          params.set("languageCode", "sp-MX");
          params.set("dealerCode", "120021");

    mvc.perform(get("/upg-admin-controller/maintenence-intervals-by-metadata")
            .contentType(MediaType.APPLICATION_JSON)
            .params(params))
            .andExpect(status().isBadRequest());
}


错误:


  错误:java.lang.AssertionError:预期状态:,但之前是:。


我一直在尝试修复它,但是找不到解决方案。在Eclipse上使用EclEmma扩展。 (对不起,如果代码不在行中。文本框很小,它将一行代码分成两行。)

这也是我正在使用的具有QueryParams的Controller代码。

@RequestMapping(value = "/maintenence-intervals-by-metadata", method = RequestMethod.GET)
public ResponseEntity<List<AdminMaintenanceIntervalReponse>> findMaintenenceIntervalsByMetadata( @QueryParam("modelYear") String modelYear,
        @QueryParam("modelCode") String modelCode, @QueryParam("engineCode") String engineCode, @QueryParam("interval") String interval ,
        @QueryParam("transmissionCode") String transmissionCode , @QueryParam("importer") String importer, @QueryParam("make") String make,
        @QueryParam("descriptionText") String descriptionText, @QueryParam("languageCode")  String languageCode, @QueryParam("dealerCode")  String dealerCode, @QueryParam("brand") String Brand) throws MPMSException {

    LOGGER.log(Level.INFO, "Entered UPGAdminServiceController, getAllMaintenenceIntervalsByMetadata");

    LOGGER.log(Level.INFO, "modelYear =" + modelYear +" modelCode = " + modelCode +" engineCode = " + engineCode +" interval = " + interval + "transmissionCode = " + transmissionCode + "importer = " + importer + "make = " + make + "descriptionText = " + descriptionText);


    List<AdminMaintenanceIntervalReponse> allMaintenanceIntervalsList = new ArrayList<AdminMaintenanceIntervalReponse>();
    try{

        Integer modelYearParam = null;

        if (modelYear!=null){
            modelYearParam = Integer.parseInt(modelYear);
        }

        Integer intervalParam = null;

        if (interval!=null){
            intervalParam = Integer.parseInt(interval);
        }

        String modelCodeParam = null;

        if (modelCode!=null){
            modelCodeParam = String.valueOf(modelCode);
        }

        String engineCodeParam = null;

        if (engineCode!=null){
            engineCodeParam = String.valueOf(engineCode);
        }
        String transmissionCodeParam = null;

        if (transmissionCode!=null){
            transmissionCodeParam = String.valueOf(transmissionCode);
        }

        Integer importerParam = null;

        if (importer!=null){
            importerParam = Integer.parseInt(importer);
        }

        String makeParam = null;

        if (make!=null){
            makeParam = String.valueOf(make);

        }

        if (descriptionText!=null){
            String.valueOf(descriptionText);
        }

        allMaintenanceIntervalsList = upgAdminMaintenanceCalcService.findMaintenanceIntervalsByMetadata(modelYearParam, modelCodeParam, engineCodeParam, intervalParam, transmissionCodeParam, importerParam, makeParam, descriptionText, languageCode, dealerCode);

    } catch(MPMSException e){
        throw e;
    } catch (Exception e) {
        throw new MPMSException(ErrorConstants.UNKNOWN.getErrorCode(), "No Data Available", ErrorConstants.UNKNOWN.toString(), e);
    }

    return new ResponseEntity<List<AdminMaintenanceIntervalReponse>>(allMaintenanceIntervalsList, HttpStatus.OK);
}


有人可以帮我解决这个问题。

最佳答案

您的/maintenence-intervals-by-metadata端点具有以下查询参数:


@QueryParam(“ modelYear”)
@QueryParam(“ modelCode”)
@QueryParam(“ engineCode”)
@QueryParam(“ interval”)
@QueryParam(“ transmissionCode”)
@QueryParam(“进口商”)
@QueryParam(“ make”)
@QueryParam(“ descriptionText”)
@QueryParam(“ languageCode”)
@QueryParam(“ dealerCode”)
@QueryParam(“品牌”)


但是您的测试是使用以下命名参数向[cc]提交[GET]请求:


params.set(“ vinModelYear”,“ 2016”);
params.set(“ vinModelCode”,“ 1633F6”);
params.set(“ vinEngineCode”,“ CZTA”);
params.set(“ interval”,“ 100000”);
params.set(“ vinTransmissionCode”,“”);
params.set(“ importerNumber”,“”);
params.set(“ makeCode”,“ V”);
params.set(“ descriptionText”,“”);
params.set(“ languageCode”,“ en-US”);
params.set(“ dealerCode”,“ 408083”);


因此,您提供的查询参数与/maintenence-intervals-by-metadata端点期望的查询参数不匹配。名称不匹配:


modelYear与vinModelYear
modelCode与vinModelCode
...等等


并且至少没有提供一个查询参数:端点声明/maintenence-intervals-by-metadata,但是您没有提供名为@QueryParam("brand")的参数。

我怀疑与400错误相关的消息可能包含类似"brand"的内容。

如果更改您的调用,以使Required String parameter '...' is not present端点定义的每个查询参数都具有正确类型(/maintenence-intervals-by-metadata)的提供的参数值,那么我认为400将不再出现。

10-05 21:28