嵌套列表的放心测试失败,java
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;
}
}
我在开发测试时遇到错误。我尝试了很多东西,但每次都失败。
这是我的 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;
}
}