Reputation: 19
I have a class that parses a .json file and I am trying to write test code for the parser method.
Here is my test code, the expected and result are the same as you can see by the trace log.
@Test
public void testParse() throws Exception {
System.out.println("parse");
String fileName = "data/fake-1.json";
RedditListingParser instance = new RedditListingParser();
ArrayList<RedditThing> expResult = new ArrayList();
RedditThing thing = new RedditThing("85osel", "ONE TWO TWO THREE THREE THREE", "JoseTwitterFan");
expResult.add(thing);
ArrayList<RedditThing> result = instance.parse(fileName);
assertEquals(expResult, result);
}
And here is the stack log
expected: java.util.ArrayList<[85osel, JoseTwitterFan, ONE TWO TWO THREE THREE THREE]> but was: java.util.ArrayList<[85osel, JoseTwitterFan, ONE TWO TWO THREE THREE THREE]>
junit.framework.AssertionFailedError
at reddithottopicsanalyser.RedditListingParserTest.testParse(RedditListingParserTest.java:55)
I'm not sure what the exact problem is here as I can implement a test to check whether it is null and that does that test without a problem.
Thankyou.
Upvotes: 1
Views: 1163
Reputation: 8851
You are comparing object references when it comes to objects inside of the two lists. In order to compare the objects correctly , you need to override the equals method. In RedditThing class , add this code.
@Override
public boolean equals(Object obj) {
if( !(obj instanceof RedditThing){
return false;
}
RedditThing redditThing2 = (RedditThing)obj;
return compareEquality(this,redditThing2);
}
static boolean compareEquality(RedditThing one, RedditThing two){
//Compare attributes of two objects here and return true/false depending
// on comparison
}
Then you can call
assertEquals(expResult, result);
Upvotes: 1