首页 > 解决方案 > 通过创建 JSON 对象使用数据生成器的空手道 DSL 返回 null

问题描述

我正在尝试构建一个动态 JSON 对象,为此我正在使用数据生成器,我将 JSON 对象的数据放入其中,并在 .feature 文件中调用该数据生成器。

URL 请求未从数据生成器获取数据并显示为空。可能的原因是什么?

import net.minidev.json.JSONObject;

public class DataGenerator {
    public static JSONObject getRandomWF(){
        String workflowId = "hello"; 
        String name = "bye";
        String description = "descc"; 
        String category = null;
        Boolean setnotify = false;
        json.put("workflowId", workflowId);
        json.put("name", name);
        json.put("description", description);
        json.put("setnotify", setnotify);
        return json;
      }
}

.feature 文件如下:

Given path '/workflow/create'
* header Authorization = 'Bearer ' + accessToken
* def dataGenerator = Java.type('DataGenerator')
* def articleRequestBody = read('classpath:examples/users/createworkflow.json')
* set articleRequestBody.workflowId = dataGenerator.getRandomWF().workflowId
* set articleRequestBody.name = dataGenerator.getRandomWF().name
* set articleRequestBody.description = dataGenerator.getRandomWF().description
* set articleRequestBody.setnotify = dataGenerator.getRandomWF().setnotify
And request articleRequestBody
When method Post
Then status 200

下面是createworkflow.json:

{
workflowId : "id workflow"
name : "name workflow"
description : "Desc workflow"
setnotify : true
}

任何有助于了解为什么每个键(workflowId、name、description、setnotify)都为 null 的任何帮助都将不胜感激。

标签: scalakarate

解决方案


您尚未阅读文档:https ://github.com/intuit/karate#calling-java

不要使用JSONObject返回类型,不管是什么,只使用一个普通的Map. 或者您可以返回一个字符串并在空手道中进行类型转换:https ://github.com/intuit/karate#type-conversion


推荐阅读