jsonObject.toStringnet.minidev.json.JSONObject;给出了空指针异常黄瓜4,在黄瓜2中工作正常

问题描述

我们有Serenity-Rest-Assured API测试,在Cucumber 2中可以正常工作,但是当尝试采用Cucumber 4时,相同的.feature文件不起作用。触发POST调用时,它在以下代码中给出了空指针异常:

一个BaseClass.java:

import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;

public static String request;
        String json = request;

         public JSONObject jsonObject = (JSONObject) JSONValue.parse(request);

在StepDeFinition文件的@When语句中:

System.out.println("Request payload is new: " + request.toString());//Gives the output of request which is valid (even gives the same output in Cucumber 2)

System.err.println("jsonobject value is:" + jsonObject.toString());//Gives null pointer exception   as given below in Cucumber 4 (getting correct value printed in Cucumber 2 tests).

错误

java.lang.NullPointerException
    at stepDeFinitions.CoreBasicStepDefi.i_trigger_post_call(CoreBasicStepDefi.java:91)

如果我评论打印声明并继续,则面临以下问题:

When i trigger post call                                                                # stepDeFinitions.CoreBasicStepDefi.i_trigger_post_call()
      io.restassured.path.json.exception.JsonPathException: Failed to parse the JSON document
at ✽.i trigger post call(file:///C:/Users/test/Documents/ProjectAPI/src/test/resources/features/testfeature/Core_POST_test.feature:12)
Caused by: java.lang.IllegalArgumentException: The JSON input text should neither be null nor empty

在这里,尽管“ request”存储了.json文件,但它仍为null。

如果我在@when方法下使jsonObject局部变量生效,则它现在可以正常工作。但是,如果尝试通过在BaseClass.java下添加来使其具有通用性,则会出现问题。

JSONObject jsonObject = (JSONObject) JSONValue.parse(request);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)