Java Junit 测试用例失败,因为它获取 String 的预期值类型和 ArrayList 的实际值类型

发布于 2025-01-11 12:17:26 字数 1907 浏览 0 评论 0原文

我正在编写 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);
    }
}

My 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。因此,我尝试将预期转换为 ArrayList,但这又添加了一个 Array,因为内容被包装在多个 Array 中。

有什么方法可以尝试仅检查内容并忽略 Junit 中的数据类型,或者有其他方法可以解决此问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2025-01-18 12:17:26

发布答案,因为它对其他人将来可能有用:

为了解决这个问题,我尝试使用 Fasterxml JacksonObjectMapper 将实际输出和预期输出转换为 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()));
    }
}

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()));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文