Issue
I am writing a Junit test case and the test case fails due to the mismatch in type. When I see the difference I see that the expected value is in String
and the Actual value is ArrayList
but the rest of everything is the same. Is there any way to compare only the content and not the type?
Following is the code that I have:
import org.junit.Test;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class MyTest {
@Test
public void SimpleTest() throws Exception {
final List<String> actualList = new ArrayList<>();
actualList.add("{ \"name\": \"Hello\",\"job\": \"Zimmermann\"}");
actualList.add("{ \"name\": \"Bye\",\"job\": \"Malen\"}");
final String expectedJson = new String(getClass().getClassLoader().getResourceAsStream("./expected.json").readAllBytes(), StandardCharsets.UTF_8);
System.out.println(expectedJson);
System.out.println(actualList);
assertEquals(expectedJson, actualList);
}
}
My expected.json:
[{ "name": "Hello","job": "Zimmermann"}, { "name": "Bye","job": "Malen"}]
Output:
[{ "name": "Hello","job": "Zimmermann"}, { "name": "Bye","job": "Malen"}]
[{ "name": "Hello","job": "Zimmermann"}, { "name": "Bye","job": "Malen"}]
java.lang.AssertionError: expected: java.lang.String<[{ "name": "Hello","job": "Zimmermann"}, { "name": "Bye","job": "Malen"}]> but was: java.util.ArrayList<[{ "name": "Hello","job": "Zimmermann"}, { "name": "Bye","job": "Malen"}]>
Expected :java.lang.String<[{ "name": "Hello","job": "Zimmermann"}, { "name": "Bye","job": "Malen"}]>
Actual :java.util.ArrayList<[{ "name": "Hello","job": "Zimmermann"}, { "name": "Bye","job": "Malen"}]>
<Click to see difference>
When I see the difference the only difference is that: expected is String and actual is ArrayList. So I tried converting the expected to ArrayList
but that's adding one more Array
due to which content is wrapped in multiple Array.
Is there any way I can try to check only for content and ignore the data type in Junit or is there any other way to resolve this issue?
Solution
Posting the answer as it can be useful to someone else in the future:
To fix the issue I tried to convert both actual and expected output into JsonNode using the ObjectMapper
of the Fasterxml Jackson
library.
Following is the same code for the same:
import com.fasterxml.jackson.databind.ObjectMapper;
public class MyTest {
private final ObjectMapper objectMapper = new ObjectMapper();
@Test
public void SimpleTest() throws Exception {
final List<String> actualList = new ArrayList<>();
actualList.add("{ \"name\": \"Hello\",\"job\": \"Zimmermann\"}");
actualList.add("{ \"name\": \"Bye\",\"job\": \"Malen\"}");
final String expectedJson = new String(getClass().getClassLoader().getResourceAsStream("./expected.json").readAllBytes(), StandardCharsets.UTF_8);
System.out.println(expectedJson);
System.out.println(actualList);
//assertEquals(expectedJson, actualList);
assertEquals(objectMapper.readTree(expectedJson), objectMapper.readTree(actualList.toString()));
}
}
Answered By - BATMAN_2008
Answer Checked By - Terry (JavaFixing Volunteer)