I'm using rest call for some testing. UI testing. After calling rest call and I want it to return fields in object in same order all the time. What can I do for that? There was assertEquals("some string which is actually o/p of rest call", actualString); in my test. But after adding one more field in class I observed that it returned rest o/p differently sometimes. So I want to have fixed order of fields in returned json, if I call rest call.
I was thinking of using json conversion and asserting on converted java object. Like below:
Gson gson = new Gson();
String json = gson.toJson(badIndicatorsdata7.getData());
MyClass indicatorDto = gson.fromJson(json , MyClass.class);
But I'm getting below exception on this line
MyClass indicatorDto = gson.fromJson(json , MyClass.class);
java.lang.NoClassDefFoundError: Lcom/ideas/tetris/pacman/services/dateservice/dto/DateParameter;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2300)
at java.lang.Class.getDeclaredFields(Class.java:1745)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:109)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
at com.google.gson.Gson.getAdapter(Gson.java:349)
at com.google.gson.Gson.fromJson(Gson.java:790)
at com.google.gson.Gson.fromJson(Gson.java:757)
at com.google.gson.Gson.fromJson(Gson.java:706)
at com.google.gson.Gson.fromJson(Gson.java:678)
at com.google.gson.Gson$fromJson.call(Unknown Source)
I want below rest o/p in always same order
[
{
"happy": true,
"social": false,
}
]
happy and social should not change their order. Is their a way?
happy and social should not change their order. Is their a way?
Well, that's just a JSON object. It's rare (IME) that the order should matter - and that doesn't look like it's what actually causes the failure you're seeing.
I would suggest that for testing purposes you have some code which, given two JSON objects, puts them into some canonical form - but that your production code shouldn't care about the order in which properties occur... that would be pretty brittle.
If you really care about ordering, I would suggest creating a list of key/value pairs instead of just a JSON object. That's meant to stay in order.
To fix the problem you're actually seeing, however, it looks like you need to find where the DateParameter
is meant to come from, and make sure that's in your classpath.
See more on this question at Stackoverflow