我目前使用TestNg編寫測試用例。我使用PodamFactory填充對象。我有以下測試用例結構。是Java jackson ObjectWriter.ObjectWriter.writeValueAsString(obj);穩定?
@Test
public void testIt(){
ARespObject resp = PodamFactory.manufacturePojo(ARespObject.class);
String responseXml = new JaxbStringTransformer().transform(resp);
// a new object with all the same data
ARespObject respActual = responder.getObj(responseXml);
Assert.assertTrue(TestUtils.areEqual(respActual , resp));
}
public static <T extends Object> boolean areEqual(T sourceObj, T target) {
if (sourceObj == null && target == null) {
return true;
}
if (sourceObj == target) {
return true;
}
if (sourceObj.getClass() != target.getClass()) {
return false;
}
if (sourceObj != null && target != null) {
return stringifyObject(sourceObj).equals(stringifyObject(target));
}
return false;
}
public static String stringifyObject(Object obj) {
String result = "";
ObjectWriter ow = new JaxbJacksonObjectMapper().writer().withDefaultPrettyPrinter();
try {
result = ow.writeValueAsString(obj);
} catch (JsonGenerationException e1) {
LOG.error(e1);
} catch (JsonMappingException e1) {
LOG.error("JsonMappingException: " + e1);
} catch (IOException e1) {
LOG.error("IOException: " + e1);
}
return result;
}
我需要知道,如果writeValueAsString(OBJ)總是兩個對象提供相同的結構(即,它的輸出將保持穩定)出發,按照
stringifyObject(sourceObj).equals(stringifyObject(target));
是一個有效的檢查。我擔心它是否會給我ARespObject內部不同的變量排序。
我會關心數組/列表中的元素順序。 –