【问题标题】:JUnit - Comparing Arrays of objectsJUnit - 比较对象数组
【发布时间】:2018-10-22 09:46:16
【问题描述】:

我有一个测试旨在测试从数据库返回的对象数组(一组使用规则包装类的规则)的内容:

[
    {
        id: 1,
        name: "rule_1",
        description: "this rule will check something",
    },
]


    @Test
public void testQueryRules() throws IOException {

    final List<Rule> rules = ruleDB.queryRules();
    final String expectedRuleId = "1";
    final String expectedName = "rule_1";
    final String expectedDescription = "this rule will check something";

    final Rule rule = new Rule(expectedRuleId, expectedName, expectedDescription);
    final List<Rule> expectedRules = new ArrayList<Rule>();
    expectedRules.add(rule);

    expectedRules.forEach(expectedRule -> {
        System.out.println("RULE ID " + expectedRule.getRuleId());
        System.out.println("RULE NAME " + expectedRule.getRuleName());
        System.out.println("RULE DESCRIPTION " + expectedRule.getDescription());
    });

    rules.forEach(actualRule -> {
        System.out.println("ACTUAL RULE ID " + actualRule.getRuleId());
        System.out.println("ACTUAL RULE NAME " + actualRule.getRuleName());
        System.out.println("ACTUAL DESCRIPTION " + actualRule.getDescription());
    });

    System.out.println("MATCHED " + Arrays.deepEquals(expectedRules.toArray(), rules.toArray()));
    System.out.println("MATCHED AGAIN " + Arrays.equals(expectedRules.toArray(), rules.toArray()));

    Assert.assertArrayEquals(expectedRules.toArray(), rules.toArray());
}

如您所见,我尝试过使用Arrays.equals()Arrays.deepEquals()assertArrayEquals()。尽管预期和实际的输出相同,但这些似乎都没有成功测试:

预期:

RULE ID: 1
RULE NAME: "rule 1",
RULE DESCRIPTION: "this rule will check something",

实际:

ACTUAL RULE ID: 1
ACTUAL RULE NAME: "rule 1",
ACTUAL RULE DESCRIPTION: "this rule will check something"

在 Java 中测试对象数组的标准方法是什么?它似乎失败了,因为即使内容相同,指针引用也不同。

【问题讨论】:

  • 什么是Rule?它会覆盖equals()吗?
  • 要完成@Oleg 注释,这并不是因为2 个对象的所有属性都相等,这2 个对象也相等。覆盖等于。
  • @Oleg 这正是它没有做的。我的规则包装类没有覆盖等于。如果您将此添加为答案,我很乐意将其标记为正确。
  • @snakespan 没关系,谢谢。我不再这样做了。

标签: java junit


【解决方案1】:

您可能想为此使用assertj。尤其是它的usingFieldByFieldElementComparator

import java.util.Arrays;
import java.util.List;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

class MyTests {
    static class Rule {
        final String id;
        final String name;
        final String description;

        Rule(String id, String name, String description) {
            this.id = id;
            this.name = name;
            this.description = description;
        }

        // no equals, no hashCode
    }

    @Test
    void rulesShouldEqual_whenTheirPropertiesEqual() {
        Rule actualRule1 = new Rule("id1", "name1", "description1");
        Rule actualRule2 = new Rule("id2", "name2", "description2");
        List<Rule> actual = Arrays.asList(actualRule1, actualRule2);

        Assertions.assertThat(actual).usingFieldByFieldElementComparator().containsExactly(
                new Rule("id1", "name1", "description1"),
                new Rule("id2", "name2", "description2"));
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2022-01-11
    • 2016-04-03
    • 2022-01-23
    相关资源
    最近更新 更多