Reputation: 407
I am using Java and testng. I have two sets of strings: validSet and actualSet. I would like to check, via assert, that everything in actualSet is in validSet (that is that actualSet (- validSet. (I am using (- to denote is a subset of). There is an assertContains(desc, collection1, object). I tried this, but it took my actualSet and put it into a string form like "[item1, item2, item3]" instead of treating it as a set.
There is an assertCollectionEquals() with two collections. I figure if I can remove everything in validSet that is not in actualSet then I could check whether the two were equal. If they are not then I know actualSet is not contained in validSet.
And I could do something like the following:
for (String act : actual ) {
assertTrue( act + " is not valid", validSet.contains(act);
}
but the nice thing about the collection asserts is they will list every string which is not in validSet while the example above will list only the first encountered and then fail.
I am trying to think of the methods to call so that comparing the two sets should be equal. How can I remove from one set all values that are NOT in another set?
Thank You
Upvotes: 1
Views: 4215
Reputation: 5463
One solution may be to build your error message using the loop and then assert after the loop with that error message. Something like:
boolean fail = false ;
List<String> actNotInValid = new ArrayList<>();
for(String act: actual) {
if(!validSet.contains(act)) {
fail = true ;
actNotInValid.add(act);
}
}
assertFalse(actNotInValid.toString(), fail);
Upvotes: 1