简体   繁体   English

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

[英]Store object for later use in java

I'm working on API testing automation using Rest assured.我正在使用放心的 API 测试自动化。 I would like to store Response as an object after calling an API so I can validate some data using that object like, status code, body, header and all.我想在调用 API 后将 Response 存储为一个对象,以便我可以使用该对象验证一些数据,例如状态代码、正文、标题等。

I tried using System.setPropery but it allows to store only String and if store Response as String like System.setProperty("test", response.toString());我尝试使用System.setPropery但它只允许存储 String 并且如果将 Response 存储为 String 就像System.setProperty("test", response.toString()); and try to retrieve System.getProperty("test");并尝试检索System.getProperty("test"); then throws the error然后抛出错误

java.lang.ClassCastException: java.lang.String cannot be cast to io.restassured.response.Response java.lang.ClassCastException: java.lang.String 不能转换为 io.restassured.response.Response

It there a way to store an object somewhere and access it for later use?有没有办法将对象存储在某处并访问它以备后用?

Don't use System.Properties for this purpose.不要为此目的使用System.Properties Please use a simple cache store as given below.请使用下面给出的简单缓存存储。

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;
    }

}

Once your execution work is completed you can remove that key.执行工作完成后,您可以删除该密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:如何将整个对象存储到外部位置并重用它来重新创建对象以供以后使用? - Java: How to store entire object to an external location and reuse it to recreate the object for later use? 在Java中存储String变量以供以后使用 - Store a String variable in Java to use it later 如何将基本信息转化为对象并存储以备后用 - How to turn basic information into and object to store for later use 如何存储类的类型以供以后在Java中使用? - How do I store the type of a class for use later in Java? 自动在外部存储字符串数据,以供以后在Java中使用 - Automatically Store String Data Externally For Later Use in Java 如何存储文本文件字符串以供以后在Java中使用 - How to store strings of text files for later use in Java 保存对象以备后用 - Saving object for later use 在运行时保存对象的实例,以后再用于测试Java - Save instance of object during runtime and use it later for testing java 如何将对象类型的数组存储为字符串,以便另一个类以后可以调用并打印该字符串。 爪哇 - How to store an array of object types as a string so another class can later call and print that string. java 使用 Java Swing,如何获取用户的输入并将其存储以备后用? - Using Java Swing, how do you take an input from a user and store it for later use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM