有一个简单的类:斯波克设置列表作为参数通过使用表
class Person {
private int age;
private String name;
public String getName(){return this.name;}
public int getAge(){return this.age;}
public void setName(String name){this.name = name;}
public void setAge(int age){this.age = age;}
}
和我有SearchPeople
接口与实现SearchPeopleImpl
一个方法getPersonNameWithPrefix()
和getPeopleNames()
:
class SearchPeopleImpl implements SearchPeople {
public String getPersonNameWithPrefix(Person person){
return "prefix" + person.getName();
}
public List<String> getPeopleNames(List<Person> peopleList){
return peopleList.stream().map(Person::getName).collect(Collectors.toList());
}
}
我想在我的测试中使用参数,它看起来像:
def 'test Person name'(){
given:
def searchPeople = new SearchPeopleImpl()
def person = Mock(Person){
getName() >> a
}
when:
def name = searchPeople.getPersonNameWithPrefix(person)
then:
name == b
where:
a | b
"AA" | "prefixAA"
"BB" | "prefixBB"
"CC" | "prefixCC"
}
它运行良好,但我有一个测试我的第二种方法的问题。如何将元素列表放入table
(在where
节中),将其用作方法参数,然后期望另一个对象列表?我的意思是我要声明Person
对象的一些列表,然后检查Strings
@UPDATE 的那个方法返回正确的列表那么,有没有办法做这样的事情:
def 'test getting persons names'(){
given:
def searchPeople = new SearchPeopleImpl()
when:
def names = searchPeople.getPeopleNames(a)
then:
names == b
where:
a | b
["AA","BB"].collect{ x -> Mock(Person){ getName() >> x } } | [["AA", "BB"]]
["CC"].collect{ x -> Mock(Person){ getName() >> x } } | [["CC"]]
["DD","EE","FD"].collect{ x -> Mock(Person){ getName() >> x } } | [["DD","EE","FD"]]
}
或:
def 'check double2 return value'(){
given:
def searchPeople = new SearchPeopleImpl()
when:
def names = searchPeople.getPeopleNames(a)
then:
names == b
where:
people1 << [
["AA","BB"].collect{ x ->
Mock(Person){
getName() >> x
}
}
]
people2 << [
["CC"].collect{ x ->
Mock(Person){
getName() >> x
}
}
]
names1 << [["AA", "BB"]]
names2 << [["CC"]]
a | b
people1 | names1
people2 | names2
}
我只是想使用表来设置参数,但它可能是我完全错了。
@UPDATE有一个错误:
check double return value[0](com.test.myPlugin.api.SearchPeopleSpec) Time elapsed: 0.125 sec <<< FAILURE!
Condition not satisfied:
names == b
| | |
| | [[AA, BB]]
| false
[AA, BB]
,并没有为每个行同样的错误。
模拟简单的pojos是不好的做法,通过使用bean构造函数,例如'[“AA”,“BB”],collect {new Person(name:it)}',可以达到同样的效果。 –