使用非原始参数进行参数化的JUnit测试? [英] Parameterized JUnit tests with non-primitive parameters?

查看:69
本文介绍了使用非原始参数进行参数化的JUnit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用参数运行JUnit测试的可能性很大,其中相同的测试方法使用不同的数据多次执行,如下所述:

There is a nice possibility to run JUnit test with parameters where the same test method is executed multiple times with different data as described here: http://junit.org/apidocs/org/junit/runners/Parameterized.html

不幸的是,似乎只能使用基本参数或字符串,而不能使用对象.有没有解决的办法?

Unfortunately, it only seems possible to use primitive parameters or Strings, but not objects. Is there any workaround known for this?

推荐答案

The type of the data() method in the use of the @Parameters annotation is List<Object[]>, so you can put in any object.

要传入例如Money对象,要转换为列表的数组将是:

To pass in, e.g., a Money object, your array to be converted to a list would be:

{{new Money(26,"CHF")}, {new Money(12,"USD")}}

{ { new Money(26, "CHF") }, { new Money(12, "USD") } }

然后,测试类的构造函数应将Money对象作为参数.

The constructor of the test class should take a Money object as argument then.

这篇关于使用非原始参数进行参数化的JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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