Java Junit 测试用例失败,因为它得到期望值类型 String 和实际值类型 ArrayList
Java Junit test case fails as it gets expected value type of String and actual value type of ArrayList
我正在编写一个 Junit 测试用例,但由于类型不匹配,测试用例失败了。当我看到差异时,我看到期望值在 String
中,实际值在 ArrayList
中,但其余的一切都是一样的。有没有办法只比较内容而不比较类型?
以下是我的代码:
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);
}
}
我的expected.json:
[{ "name": "Hello","job": "Zimmermann"}, { "name": "Bye","job": "Malen"}]
输出:
[{ "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>
当我看到区别时,唯一的区别是:预期是 String,实际是 ArrayList。所以我尝试将 expected 转换为 ArrayList
但由于内容被包装在多个数组中,因此又添加了一个 Array
。
有什么方法可以尝试只检查内容而忽略 Junit 中的数据类型,或者有没有其他方法可以解决这个问题?
发布答案,因为它可能对以后的其他人有用:
为了解决这个问题,我尝试使用 Fasterxml Jackson
库的 ObjectMapper
将实际输出和预期输出都转换为 JsonNode。
以下是相同的代码:
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()));
}
}
我正在编写一个 Junit 测试用例,但由于类型不匹配,测试用例失败了。当我看到差异时,我看到期望值在 String
中,实际值在 ArrayList
中,但其余的一切都是一样的。有没有办法只比较内容而不比较类型?
以下是我的代码:
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);
}
}
我的expected.json:
[{ "name": "Hello","job": "Zimmermann"}, { "name": "Bye","job": "Malen"}]
输出:
[{ "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>
当我看到区别时,唯一的区别是:预期是 String,实际是 ArrayList。所以我尝试将 expected 转换为 ArrayList
但由于内容被包装在多个数组中,因此又添加了一个 Array
。
有什么方法可以尝试只检查内容而忽略 Junit 中的数据类型,或者有没有其他方法可以解决这个问题?
发布答案,因为它可能对以后的其他人有用:
为了解决这个问题,我尝试使用 Fasterxml Jackson
库的 ObjectMapper
将实际输出和预期输出都转换为 JsonNode。
以下是相同的代码:
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()));
}
}