[英]Rest-assured test failed with nested lists, java
我面臨着將開發我的測試的錯誤。 我嘗試了一堆東西,但每次都失敗。
這是我的JSON:
{
"evenement": [
{
"dateDebut": "2016-02-20T00:00:00-05:00",
"dateFin": "2016-02-21T00:00:00-05:00",
"id": "28",
"tirages": [
{
"dateDebut": "2016-02-20T00:00:00-05:00",
"dateFin": "2016-02-20T00:00:00-05:00",
"description": "Un super prix1",
"id": "27",
"titre": "A Gagner1"
},
{
"dateDebut": "2016-02-21T00:00:00-05:00",
"dateFin": "2016-02-21T00:00:00-05:00",
"description": "Un super prix2",
"id": "28",
"titre": "A Gagner2"
}
]
},
{
"dateDebut": "2016-03-20T00:00:00-04:00",
"dateFin": "2016-03-21T00:00:00-04:00",
"id": "29",
"tirages": {
"dateDebut": "2016-03-20T00:00:00-04:00",
"dateFin": "2016-03-20T00:00:00-04:00",
"description": "Un super prix3",
"id": "29",
"titre": "A Gagner3"
}
}
]}
這是我的測試:
ArrayList<String> alDescr1 = new ArrayList<String>();
alDescr1.add("Un super prix1");
alDescr1.add("Un super prix2");
ArrayList<Object[]> alDescr2 = new ArrayList<Object[]>();
alDescr2.add(alDescr1.toArray());
ArrayList<String> alDescr3 = new ArrayList<String>();
alDescr3.add("Un super prix3");
alDescr2.add(alDescr3.toArray());
expect().statusCode(200)
.body(
"evenement.tirages.description", hasItems(alDescr1.toArray(), alDescr2.toArray())
).when().get("/rest/amc-events/all");
這是錯誤:
java.lang.AssertionError: 1 expectation failed.
JSON path evenement.tirages.description doesn't match.
Expected: (a collection containing ["Un super prix1", "Un super prix2"] and a collection containing [["Un super prix1", "Un super prix2"], ["Un super prix3"]])
Actual: [[Un super prix1, Un super prix2], Un super prix3]
實際上,我嘗試檢查某個元素是否具有[Un super prix1,Un super prix2],而另一個元素是否具有“ Un super prix3”。 但是,如果有一種方法可以僅檢查元素是否存在,而無論第一個元素是否包含2個元素,那都很好。
謝謝瓦爾
很好,我在這里找到了解決方案: http : //tugdualgrall.blogspot.ca/2011/09/jax-rs-jersey-and-single-element-arrays.html
import com.grallandco.employee.service.converter.EmployeeConverter;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import javax.xml.bind.JAXBContext;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.api.json.JSONJAXBContext;
@Provider
public class JAXBContextResolver implements ContextResolver < JAXBContext > {
private JAXBContext context;
private Class[] types = {EmployeeConverter.class, Class1.class, Class2.class};
public JAXBContextResolver() throws Exception {
this.context = new JSONJAXBContext(JSONConfiguration.mapped().arrays("employee").build(),
types);
}
public JAXBContext getContext(Class objectType) {
for (Class type : types) {
if (type == objectType) {
return context;
}
}
return null;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.