首页 > 解决方案 > 使用 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 单元测试类?

标签: javaunit-testingspring-mvctestng

解决方案


推荐阅读