gpt4 book ai didi

java - JUnit 测试(正向和负向测试)

转载 作者:太空宇宙 更新时间:2023-11-04 11:02:57 27 4
gpt4 key购买 nike

这是我为 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());
}

错误:

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

我一直在尝试修复它,但找不到解决方案。在 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("间隔")
  • @QueryParam("传输代码")
  • @QueryParam("导入者")
  • @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("间隔", "100000");
  • params.set("vinTransmissionCode", "");
  • params.set("进口商编号", "");
  • params.set("makeCode", "V");
  • params.set("descriptionText", "");
  • params.set("languageCode", "en-US");
  • params.set("dealerCode", "408083");

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

  • modelYear 与 vinModelYear
  • modelCode 与 vinModelCode
  • ...等

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

我怀疑与 400 错误相关的消息可能包括以下内容:所需的字符串参数“...”不存在

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

关于java - JUnit 测试(正向和负向测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46696421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com