问题描述
使用参数运行JUnit测试的可能性很大,其中相同的测试方法使用不同的数据多次执行,如下所述: http://junit.org/apidocs/org/junit/runners/Parameterized.html
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?
推荐答案
在注释为List<Object[]>
,因此您可以放入任何对象.
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:
然后,测试类的构造函数应将Money对象作为参数.
The constructor of the test class should take a Money object as argument then.
这篇关于使用非原始参数进行参数化的JUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!