使用 PAssert containsInAnyOrder() 比较对象的测试管道 [英] Test pipeline comparing objects using PAssert containsInAnyOrder()

查看:24
本文介绍了使用 PAssert containsInAnyOrder() 比较对象的测试管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Apache Beam 管道编写 JUnit 测试.我开始使用 DoFnTester,但它已被弃用,尽管 Apache Beam测试您的管道"文档中的示例指的是已被弃用的方法.

I'm writing JUnit tests for an Apache Beam pipeline. I started using DoFnTester but that's been deprecated although the examples in the Apache Beam "Test Your Pipeline" documentation refer to methods that have since been deprecated.

我现在正在使用推荐的 TestPipeline 和 PAssert,但在使用 PAssert 时遇到困难,因为我见过的所有示例都使用字符串.

I'm now using the recommended TestPipeline and PAssert but am having difficulty with the PAssert as all the examples I've seen use Strings.

管道输出对象,因此作为比较对象的测试失败.我的第一直觉是创建一个派生对象并覆盖 equals().我无法让它工作(我只使用 Java 几个星期,所以也许这没有帮助)并且测试仍在调用原始的 equals() 方法.

The pipeline outputs objects so the test fails as its comparing objects. My first instinct was to create a derived object and override equals(). I couldn't get that to work (I've only been using Java for a couple of weeks so maybe that doesn't help) and the test was still calling the original equals() method.

然后我想到在使用测试管道时迭代输出 PCollection,管道中最多只有一两个元素,但我找不到任何有关如何执行此操作的示例.不确定这是否是正确的测试方式.

I then thought of iterating through the output PCollection as I was using the test pipeline and there would only be one or two elements at most in the pipeline but I couldn't find any examples of how to do this. Not sure if this is the right way to test anyway.

我已经通读了 Apache 文档,发现此注释PAssert 对象不支持 Object.equals(Object).如果您打算测试对象相等性,请改用 containsInAnyOrder(T...) 的变体.".这听起来正是我需要的,但问题是我不知道如何创建 containsInAnyOrder() 的变体来执行此操作,而且我找不到任何示例.

I've read through the Apache documentation and found this comment "Object.equals(Object) is not supported on PAssert objects. If you meant to test object equality, use a variant of containsInAnyOrder(T...) instead.". That sounds exactly what I need but the problem is I don't know how to create a variant of containsInAnyOrder() to do this and I couldn't find any examples.

我用完了谷歌的东西!许多示例已经过时(包括 Apache 自己的文档)引用已被弃用的方法.我在 PAssert 中找到的所有示例都假定输出元素是字符串.

I've run out of things to google! A lot of the examples are out of date (including Apaches own documentation) referencing methods that have been deprecated. All the examples I've found with PAssert assume the output elements are Strings.

我需要做的就是覆盖 containsInAnyOrder 以便我可以进行自己的对象比较.有人可以帮忙吗?

All I need to do is override containsInAnyOrder so I can do my own object comparison. Can anyone help?

PAssert.that(output).containsInAnyOrder(expected);

如果有人能指点我一些例子,我将不胜感激.

If anyone could point me to some examples that would be much appreciated.

推荐答案

覆盖对象类中的 equals 方法以适当地比较两个对象.Assert 只是使用 equals 方法来比较对象.请参阅此页面了解如何覆盖equals.

Override the equals method in your object's class to compare two objects appropriately. Assert is simply using the equals method to compare the objects. Refer to this page for how to override equals.

这篇关于使用 PAssert containsInAnyOrder() 比较对象的测试管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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