0

in the loop , I make some actions then get and add string to the list1 after loop: list1 contains 2 strings : OK now, in another loop I get value on each iteration which return me a string and I would like to check that strings in list1 aren't available in the second:

for(inti=0;i<nb;i++){
   value= table.getValue(i);
   list1.add(value);
}

for(int j=0;j<nb;j++){
   String toto = table.getValue(i);
   //do other actions

   //here verify that list1 doesn't contain toto
   //I tried :
   assetFalse(table.getValue(i).contains(list1)); //not OK !
}
John B
  • 32,493
  • 6
  • 77
  • 98
lola
  • 5,649
  • 11
  • 49
  • 61

5 Answers5

1

Seems like it should be

assertFalse(list1.contains(table.getValue(i)));

If getValue returns a String, you cannot do a contains operation on it passing a List.

John B
  • 32,493
  • 6
  • 77
  • 98
1
//here verify that list1 doesn't contain toto
//I tried :
assetFalse(table.getValue(i).contains(list1)); //not OK !

First the method is called assertFalse.

Second you're checking if toto contains the list ( table.getValue(i) is toto). Note that your code could also be read as assertFalse(toto.contains(list1)); (with the spelling fixed and using the assignment above).

Instead I guess you'd like to do this instead:

 assertFalse(list1.contains(toto));

Also note that in your two loops you iterate over the same collection (table) and the same indices ( 0 to nb-1). Thus list will always contain the values you check for (unless you're removing them somewhere) and thus your assertation will always fail.

Finally, please post compilable code: inti=0; won't compile as well as assetFalse(...) nor String toto = table.getValue(i);, since i isn't known in that loop (it is out of scope).

If you want us to help you, please put more effort into your questions.

Edit

If you want to compare two collections, you could also use a utility library like Apache Commons Collections, which has methods like CollectionUtils.containsAny(collection1, collection2) (this would return true if at least one element is present in both collections) etc.

Google Guava should have similar utilities.

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

Seems that you are trying to check if a list (list1) is contained in a string. That does not make sense. Try it vice versa.

Andreas
  • 2,211
  • 1
  • 18
  • 36
  • I would like to make a list in the second loop then verify that content of list1 isn't available in list2 – lola Sep 30 '11 at 12:33
0

If you're just comparing lists of strings a simple contains() method should work.

for(String value: list2){

   //here verify that list1 doesn't contain string in list2
   if(list1.contains(value)){
      //Do something here.
   }
}
Russell Shingleton
  • 3,176
  • 1
  • 21
  • 29
0

You are referencing "i" in the second loop but it is declared in the first loop.

sogukk
  • 37
  • 1
  • 2
  • 11