You have a List of Maps. Each Map has N keys, and you state you want to make sure there are no duplicate values in each map.
1) get the values for each Map
2) instantiate a HashSet and add the values
3) verify the length of the set is N
Вопрос
I am trying to jUnit one of my method which returns List of Map.
List<Map<String, String>> mappings = TestUtils.getMappings(maps, debug);
System.out.println(mappings);
Below is the ouput I am getting -
[{dc1=h1, dc2=h2, dc3=h3},
{dc1=h4, dc2=h5, dc3=h6},
{dc1=h7, dc2=h8, dc3=h9},
{dc1=h10, dc2=h11, dc3=h1}, I am trying to jUnit one of my method which returns `List of Map<String, String>` -
List<Map<String, String>> mappings = TestUtils.getMappings(maps, debug);
System.out.println(mappings);
Below is the ouput I am getting -
[{dc1=h1, dc2=h2, dc3=h3},
{dc1=h4, dc2=h5, dc3=h6},
{dc1=h7, dc2=h8, dc3=h9},
{dc1=h10, dc2=h11, dc3=h1},
{dc1=h2, dc2=h1, dc3=h4},
{dc1=h5, dc2=h4, dc3=h7},
{dc1=h8, dc2=h7, dc3=h10},
{dc1=h11, dc2=h10, dc3=h2},
{dc1=h3, dc2=h6, dc3=h8},
{dc1=h9, dc2=h3, dc3=h5},
{dc1=h6, dc2=h9, dc3=h11}]
I am trying to to see whether each Map has correct values or not.. In short each Map will have values like this -
dc1=h9, dc2=h3, dc3=h5
So I need to junit, that value of dc1
, dc2
and dc3
are different than each other in the same map.
So if the map is like this -
dc1=h1, dc2=h2, dc3=h1
Then my assertion should fail as value of dc1 is there in dc3 as well..
Now I am not sure what is the best to jUnit this? {dc1=h2, dc2=h1, dc3=h4}, {dc1=h5, dc2=h4, dc3=h7}, {dc1=h8, dc2=h7, dc3=h10}, {dc1=h11, dc2=h10, dc3=h2}, {dc1=h3, dc2=h6, dc3=h8}, {dc1=h9, dc2=h3, dc3=h5}, {dc1=h6, dc2=h9, dc3=h11}]
Now I am not sure what is the best to jUnit this? Actually I am facing problem in doing jUnit for a Map
I am trying to to see whether each Map has correct values or not.. In short each Map will have values like this -
dc1=h9, dc2=h3, dc3=h5
So I need to junit, that value of dc1
, dc2
and dc3
are different than each other in the map.
So if the map is like this -
dc1=h1, dc2=h2, dc3=h1
Then my assertion should fail as value of dc1 is there in dc3 as well..
UPDATE:-
For some other input, I would see output like this -
[{dc1=h1, dc2=h2, dc3=h3},
{dc1=h2, dc2=h1},
{dc1=h3, dc2=h4, dc3=h2},
{dc2=h5, dc3=h1},
{dc2=h3},
{dc2=h6},
{dc2=h7},
{dc2=h8},
{dc2=h9},
{dc2=h10},
{dc2=h11}]
Решение
You have a List of Maps. Each Map has N keys, and you state you want to make sure there are no duplicate values in each map.
1) get the values for each Map
2) instantiate a HashSet and add the values
3) verify the length of the set is N
Другие советы
Try the following code:
Verifies all values are unique:
for(Map<String, String> eachMap : mapList) {
Collection<String> values = eachMap.values();
Set<String> valuesAsSet = new HashSet<>(values);
assertEquals(values.size(), valuesAsSet.size());
}
If you need to verify order:
for(Map<String, String> eachMap : mapList) {
Collection<String> values = eachMap.values();
Set<String> valuesAsSet = new HashSet<>(values);
Iterator<String> expectedIt = values.iterator();
Iterator<String> actualIt = valuesAsSet.iterator();
boolean expectedHasNext = expectedIt.hasNext();
boolean actualHasNext = actualIt.hasNext();
// Iterators are consistent
assertEquals(expectedHasNext, actualHasNext);
if(expectedHasNext) {
if(expectedHasNext) {
String expected = expectedIt.next();
String actual = actualIt.next();
assertEquals(expected, actual);
}
}
}