[英]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.