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

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

问题描述

我正在为Apache Beam管道编写JUnit测试.我开始使用DoFnTester,但尽管Apache Beam测试管道"文档中的示例引用的方法自那以后就已被弃用,但该方法已被弃用.

我现在正在使用推荐的TestPipeline和PAssert,但是在PAssert方面遇到了困难,因为我所看到的所有示例都使用字符串.

管道输出对象,因此测试作为比较对象失败.我的第一个直觉是创建一个派生对象并覆盖equals().我无法使它正常工作(我只使用Java几周了,所以也许无济于事),而测试仍在调用原始的equals()方法.

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

我通读了Apache文档,发现以下注释:"PAssert对象不支持Object.equals(Object).如果要测试对象的相等性,请改用containsInAnyOrder(T ...)的变体. ".听起来确实是我需要的,但问题是我不知道如何创建containsInAnyOrder()的变体来做到这一点,而且我找不到任何示例.

我已经用完了很多东西给谷歌!许多示例已过时(包括Apache自己的文档),它们已不推荐使用.我在PAssert中找到的所有示例都假定输出元素为字符串.

我需要做的就是重写containsInAnyOrder,这样我就可以进行自己的对象比较.有人可以帮忙吗?

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

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

解决方案

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

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.

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.

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.

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.

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.

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.

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.

解决方案

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天全站免登陆