java - 使用 testNG 进行 Spring mvc rest Controller 单元测试
问题描述
我在 Spring mvc 应用程序中有一个 rest 控制器,如下所示:我在网上搜索了很多示例,但其中大多数都使用 junit。我想用 testNg 进行单元测试
公共类 SomeController {
@Autowired
private GeneralManager generalManager;
@Autowired
private DataManager dataManager;
@RequestMapping(value="/submit", method = RequestMethod.POST, produces="application/json")
public Object processSubmit(@RequestBody String inputData, @RequestHeader("authtoken") String atoken) {
MessageEntity msgObj = ""
if(generalManager.checktoken(atoken)){
try{
return dataManager.processSubmit(inputData);
}catch (Exception e) {
logger.error("Exception occurred in submitting the data",e);
msgObj.setMessageId(1);
msgObj.setMessage("Exception in processSubmit. Failed to submit the data.");
}
}else{
return new "Exception"
}
return msgObj;
}
我需要对上面的代码进行单元测试。输入是 Json 字符串(大 Json 有效负载)。如何在我的 Spring mvc 应用程序中使用 testNG 对上述控制器进行单元测试?
在实际方法中,我通过提取 json 有效负载来获取输入数据字符串(即 Json 有效负载)并构建实体。如何将此 json 字符串(有效负载)传递给 testNG 单元测试类?
解决方案
推荐阅读
- javascript - VS2017中的javascript验证在脚本以外的其他文件夹中
- php - 如何在 PHP 照片上传脚本功能中添加文字或徽标水印?
- itext - Itext 7 .net:填充 PdfChoiceFormField
- html - 第一个焦点上的文本区域行为
- android - Android webView滚动问题
- jquery - 检查链接是否包含完全匹配的文本
- mysql - 在 SQL 中进行排名时,“+”号是什么意思?
- bash - 备份脚本:删除旧文件不起作用
- sqlite - 通过 H2 控制台访问时无法在 SQLlite 中执行查询 - SQLite JDBC 驱动程序未实现 null/0
- angular - 带有 JqueryUI Draggable 的 Angular5.x - 触发停止事件时 UI 不会更新