JUnit测试(正面和负面测试)

时间:2017-10-11 19:52:22

标签: java unit-testing spring-mvc junit eclemma

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

@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:预期状态:&lt; 202&gt;但是:&lt; 400&gt;。

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

这也是我正在使用的具有QueryParams的控制器代码。

@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);
}

有人可以帮我纠正这个问题。

1 个答案:

答案 0 :(得分:0)

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

  • @QueryParam( “modelYear”)
  • @QueryParam(“modelCode”)
  • @QueryParam(“engineCode”)
  • @QueryParam(“interval”)
  • @QueryParam(“transmissionCode”)
  • @QueryParam(“importer”)
  • @QueryParam(“make”)
  • @QueryParam( “descriptiontext描述”)
  • @QueryParam( “语言代码”)
  • @QueryParam( “dealerCode”)
  • @QueryParam( “品牌”)

但您的测试是使用以下命名参数向/maintenence-intervals-by-metadata提交[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 vs. vinModelYear
  • modelCode vs. vinModelCode
  • ...... etc

并未提供至少一个查询参数:端点声明@QueryParam("brand")但您未提供名为"brand"的参数。

我怀疑与400错误相关的消息可能包含:Required String parameter '...' is not present

如果更改调用,使/maintenence-intervals-by-metadata端点定义的每个查询参数都具有正确类型的提供参数值(String),那么我认为400将不再发生。