Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
存储对象以供以后在java中使用_Java_Rest Assured - Fatal编程技术网

存储对象以供以后在java中使用

存储对象以供以后在java中使用,java,rest-assured,Java,Rest Assured,我正在使用Rest-assured进行API测试自动化。我想在调用API后将响应存储为一个对象,这样我就可以使用该对象验证一些数据,如状态码、正文、标题和所有内容 我尝试使用System.setProperty,但它只允许存储字符串,如果将响应存储为类似System.setProperty的字符串(“test”,Response.toString())并尝试检索System.getProperty(“test”)然后抛出错误 java.lang.ClassCastException:无法将jav

我正在使用Rest-assured进行API测试自动化。我想在调用API后将响应存储为一个对象,这样我就可以使用该对象验证一些数据,如状态码、正文、标题和所有内容

我尝试使用
System.setProperty
,但它只允许存储字符串,如果将响应存储为类似
System.setProperty的字符串(“test”,Response.toString())
并尝试检索
System.getProperty(“test”)然后抛出错误

java.lang.ClassCastException:无法将java.lang.String转换为 io.restassured.response.response


是否有办法将对象存储在某个位置并访问它以供以后使用?

不要为此使用
System.Properties
。 请使用下面给出的简单缓存存储

public class ResponseCache {
    private static final ResponseCache myInstance = new ResponseCache();

    private final Map<String, Response> cacheStore = new HashMap<>();

    private ResponseCache() {
    }

    public static ResponseCache getInstance() {
        return myInstance;
    }

    public void addResponse(String key, Response value) {
        cacheStore.put(key, value);
    }

    public boolean exists(String key) {
        return cacheStore.containsKey(key);
    }

    public void remove(String key) {
        if (exists(key)) {
            cacheStore.remove(key);
        }
    }

    public Response get(String key) {
        return exists(key) ? cacheStore.get(key) : null;
    }

}
公共类响应缓存{
私有静态最终ResponseCache myInstance=新ResponseCache();
private final Map cacheStore=new HashMap();
私人回应{
}
公共静态响应缓存getInstance(){
返回myInstance;
}
public void addResponse(字符串键、响应值){
cacheStore.put(键、值);
}
存在公共布尔值(字符串键){
返回cacheStore.containsKey(键);
}
公共无效删除(字符串键){
如果(存在(键)){
cacheStore.remove(键);
}
}
公共响应获取(字符串键){
return exists(key)?cacheStore.get(key):null;
}
}

执行工作完成后,您可以删除该密钥。

不要为此使用
System.Properties
。 请使用下面给出的简单缓存存储

public class ResponseCache {
    private static final ResponseCache myInstance = new ResponseCache();

    private final Map<String, Response> cacheStore = new HashMap<>();

    private ResponseCache() {
    }

    public static ResponseCache getInstance() {
        return myInstance;
    }

    public void addResponse(String key, Response value) {
        cacheStore.put(key, value);
    }

    public boolean exists(String key) {
        return cacheStore.containsKey(key);
    }

    public void remove(String key) {
        if (exists(key)) {
            cacheStore.remove(key);
        }
    }

    public Response get(String key) {
        return exists(key) ? cacheStore.get(key) : null;
    }

}
公共类响应缓存{
私有静态最终ResponseCache myInstance=新ResponseCache();
private final Map cacheStore=new HashMap();
私人回应{
}
公共静态响应缓存getInstance(){
返回myInstance;
}
public void addResponse(字符串键、响应值){
cacheStore.put(键、值);
}
存在公共布尔值(字符串键){
返回cacheStore.containsKey(键);
}
公共无效删除(字符串键){
如果(存在(键)){
cacheStore.remove(键);
}
}
公共响应获取(字符串键){
return exists(key)?cacheStore.get(key):null;
}
}

执行工作完成后,您可以删除该键。

将对象存储在地图中?存储多长时间?在程序运行之间?将其写入文件或数据库。当程序运行时?记住它就行了。如果两者都不是,则a将有助于.store,直到执行继续。因为在执行新的请求之前,值可能需要在其他测试用例中存储对象?存储多长时间?在程序运行之间?将其写入文件或数据库。当程序运行时?记住它就行了。如果两者都不是,则a将有助于.store,直到执行继续。因为在执行新请求之前,值可能需要在其他测试用例中使用