代码之家  ›  专栏  ›  技术社区  ›  snakespan

junit-比较对象数组

  •  0
  • snakespan  · 技术社区  · 6 年前

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

    [
        {
            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中测试对象数组的标准方法是什么?它似乎失败了,因为指针引用不同,即使内容相同。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Frank Neblung    6 年前

    你可能想用 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"));
        }
    }