I've got a problem with testing Java code using Groovy/Spock. I wrote a short lambda-expression for filtering a list:
public List<myVO> getMeSomething() {
return RequestUtils.getRequestAttribut("somestuff", () -> getList().stream().filter(mt ->
!mt.isArchived()).filter(mt -> !mt.isSomething()).collect(toList()));
}
I used this to test it:
when:
2 * contextData.getList() >> [item1, item2]
List<myVO> list = context.getMeSomething()
then:
list.contains(item1)
!list.contains(item2)
This worked when run as single test. When I executed all tests in the project this one failed. I tried to extract the "getList()" into a local variable but it had no effect.
Then I changed the method to:
public List<myVO> getMeSomething() {
List<myVO> result = new ArrayList<>();
for (myVO vo : Objects.requireNonNull(getList())) {
if (!vo.isSomething() && !vo.isArchived()) {
result.add(vo);
}
}
return result;
}
And suddenly the test worked where it failed before. Is there any reason for this behavior? Is there a problem with testing a lambda with Spock?
Thanks!
Aucun commentaire:
Enregistrer un commentaire