JUnit-比较对象数组 [英] JUnit - Comparing Arrays of objects

查看:553
本文介绍了JUnit-比较对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a test that is intended to test the contents of an Array of objects (a set of rules that use a Rule wrapper class) that get returned from a DB:

[
    {
        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().即使预期和实际的输出都相同,这些似乎也都无法给出成功的测试:

As you can see, I've tried using Arrays.equals(), as well as Arrays.deepEquals(), and assertArrayEquals(). None of those seem to give a successful test even though the output of both the expected and actual is the same:

预期:

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

What is the standard way to test arrays of objects in Java? It seems like its failing because the pointer reference is different even though the contents is the same.

推荐答案

您可能想使用

You might want to use assertj for that. Especially its 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"));
    }
}

这篇关于JUnit-比较对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆