简体   繁体   中英

JUnit testing (positive and negative testing)

This is the code I have written for JUnit Testing for positive and negative 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());
}

Error:

Error: java.lang.AssertionError: Status expected:<202> but was:<400>.

I have been trying to fix it but cannot find a solution. Using EclEmma extension on Eclipse. (sorry if the code is out of line. The text box is small it splits one line of code into two lines.)

Also this is the Controller code that I am working with that has the 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);
}

Can someone please help me correct this issue.

Your /maintenence-intervals-by-metadata endpoint has the following query parameters:

  • @QueryParam("modelYear")
  • @QueryParam("modelCode")
  • @QueryParam("engineCode")
  • @QueryParam("interval")
  • @QueryParam("transmissionCode")
  • @QueryParam("importer")
  • @QueryParam("make")
  • @QueryParam("descriptionText")
  • @QueryParam("languageCode")
  • @QueryParam("dealerCode")
  • @QueryParam("brand")

But your test is submitting a [GET] request to /maintenence-intervals-by-metadata with the following named parameters:

  • 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");

So, the query params you supply do not match the query params expected by the /maintenence-intervals-by-metadata endpoint. There are name mismatches:

  • modelYear vs. vinModelYear
  • modelCode vs. vinModelCode
  • ... etc

And at least one query parameter is not supplied: the endpoint declares @QueryParam("brand") but you are not supplying a parameter named "brand" .

I suspect the message associated with the 400 error might include something like: Required String parameter '...' is not present .

If you change your invocation such that every one of the query parameters defined by the /maintenence-intervals-by-metadata endpoint has a supplied parameter value of the correct type (a String ) then I think the 400 will no longer occur.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM