使用带有通配符的assertArrayEquals()?

发布于 2024-12-11 11:48:50 字数 1162 浏览 0 评论 0原文

我想测试生成用于作为 UDP 数据包发送的字节数组的代码。

虽然我无法重现测试中的每个字节(例如随机字节、时间戳),但我想测试我可以预先确定的字节。

使用 JUnit 4.8(和 Mockito 1.8)是否可以实现类似以下内容?

Packet packet = new RandomPacket();

byte[] bytes = new byte[] {
    0x00, 0x02, 0x05, 0x00, anyByte(), anyByte(), anyByte(), anyByte(), 0x00
};

assertArrayEquals(packet.getBytes(), bytes);

上面的示例当然不起作用,我只是在寻找一种在 assertArrayEquals() 中使用某种通配符的方法。

PS:我现在唯一的选择是单独检查每个字节(并省略随机字节)。但这非常乏味并且不能真正重用。


感谢 JB Nizet 的回答,我现在有了以下代码,工作得很好:

private static int any() {
    return -1;
}

private static void assertArrayEquals(int[] expected, byte[] actual) {
    if(actual.length != expected.length) {
        fail(String.format("Arrays differ in size: expected <%d> but was <%d>", expected.length, actual.length));
    }

    for(int i = 0; i < expected.length; i ++) {
        if(expected[i] == -1) {
            continue;
        }

        if((byte) expected[i] != actual[i]) {
            fail(String.format("Arrays differ at element %d: expected <%d> but was <%d>", i, expected[i], actual[i]));
        }
    }
}

I want to test code that produces byte arrays used to send as UDP packets.

Although I'm not able to reproduce every byte in my test (e.g. random bytes, timestamps), I'd like to test the bytes that I can predetermine.

Is something like the following possible using JUnit 4.8 (and Mockito 1.8)?

Packet packet = new RandomPacket();

byte[] bytes = new byte[] {
    0x00, 0x02, 0x05, 0x00, anyByte(), anyByte(), anyByte(), anyByte(), 0x00
};

assertArrayEquals(packet.getBytes(), bytes);

The sample above is of course not working, I'm just searching for a way to use some sort of wildcard in assertArrayEquals().

PS: My only alternative right now is to check each byte individually (and omit random ones). But this is quiet tedious and not really reusable.


Thanks to the answer from JB Nizet I have the following code in place now, working just fine:

private static int any() {
    return -1;
}

private static void assertArrayEquals(int[] expected, byte[] actual) {
    if(actual.length != expected.length) {
        fail(String.format("Arrays differ in size: expected <%d> but was <%d>", expected.length, actual.length));
    }

    for(int i = 0; i < expected.length; i ++) {
        if(expected[i] == -1) {
            continue;
        }

        if((byte) expected[i] != actual[i]) {
            fail(String.format("Arrays differ at element %d: expected <%d> but was <%d>", i, expected[i], actual[i]));
        }
    }
}

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

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

发布评论

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

评论(2

甜心小果奶 2024-12-18 11:48:50

您可以简单地将所需的数组写为整数数组,并使用特殊值(例如 -1)来表示通配符。这与输入流的读取方法相同。您只需编写自定义的 assertEqualsWithWildCard(int[]预期, byte[]实际)

You could simply write your expected array as an array of integers, and use a special value (such as -1) to represent the wildcard. It's the same trick as the read methods of the input streams. You would just have to write your custom assertEqualsWithWildCard(int[] expected, byte[] actual).

記憶穿過時間隧道 2024-12-18 11:48:50

如果您要编写大量这样的代码,我会编写一个单独的类来将数据包“解码”为有意义的字段。然后(当然,在测试类本身是否有效之后)您可以编写诸如此类的合理测试。

assertEquals(42, packet.length());
assertEquals(0xDEADBEEF, packet.checksum());

这样

,您就不会“省略随机字节”,并且您的代码将更具可读性(如果稍微冗长一点)。

If you are going to be writing a lot of code like this, I would write a separate class to "decode" the packet into meaningful fields. Then (of course, after testing that the class itself works) you can write sensible tests like

assertEquals(42, packet.length());
assertEquals(0xDEADBEEF, packet.checksum());

etc.

That way, you are not "omitting random bytes", and your code will be much more readable (if a tad more verbose).

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