JUnit and Guava comparing list equality after conversion ()

In the jUnit test, I want to get some rows from a database based on the name column. Then I want to check that the lines that I have have the names that I expected. I have the following:

 Set<MyClass> typesToGet = MyClassFactory.createInstances("furniture", "audio equipment"); Collection<String> namesToGet = Collections2.transform(typesToGet, new NameFunction<MyClass, String>()); List<MyClass> typesGotten = _svc.getAllByName(typesToGet); assertThat(typesGotten.size(), is(typesToGet.size())); Collection<String> namesGotten = Collections2.transform(typesGotten, new NameFunction<ItemType, String>()); assertEquals(namesToGet, namesGotten); // fails here 

I am currently receiving this failure:

java.lang.AssertionError: expected: com.google.common.collect.Collections2 $ TransformedCollection <[audio equipment, furniture]> but it was: com.google.common.collect.Collections2 $ TransformedCollection <[audio equipment, furniture]>

So, what is the cleanest, shortest way to verify that I got rows from the database, the name column matches the names that I said I wanted? I could loop through the for loop and verify that each name in one list exists in another, but I was hoping to be more concise. Something like the following pseudo code would be nice:

 List<MyClass> typesGotten = ...; ["furniture", "audio equipment"].equals(typesGotten.map(type => type.getName())) 
+7
source share
4 answers

You can use containsAll() two times to make sure you don't have a missing value or any unexpected value.

 assertTrue(namesToGet.containsAll(namesGotten)); assertTrue(namesGotten.containsAll(namesToGet)); 

But if you decide to use List or Set instead of Collection , then the interface contract states that a List is equal to another List ( the same for Set ) iff both contain the same values .

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists are the same size, and all the corresponding pairs of elements in these two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)) .) In other words, two lists are defined equal if they contain the same elements in the same okay. This definition ensures that the equals method works correctly in different implementations of the List interface.


Resources

+15
source

If you expect them to contain the same elements, but not necessarily in the same order, just make a copy of ImmutableSet both of them and make sure that these sets are equal. If you expect them to be in the same order, make an ImmutableList copy and make sure they are equal.

Collection has no concept of equality at all.

+6
source

The most accurate and expressive way of writing such a statement is with a Hamcrest match:

 assertThat(namesGotten, containsInAnyOrder(namesToGet)) 
+6
source

Guava has a method that seems to me to convey well the concept you are trying to get: symmetricDifference . If the symmetric difference is empty, the sets are equal.

 assetTrue(Sets.symmetricDifference(namesToGet, namesGotten).isEmpty()); 

However, this may not be the β€œcheapest” one since it performs the union, intersection, and difference operation. You can also check if the sets are the same size, if they are not, they do not contain the same elements, and if they are, you can make sure that the (asymmetric) difference is empty.

 assertEquals(namesToGet.size(), namesGotten.size()); assertTrue(Sets.difference(namesToGet, namesGotten)); 
+3
source

All Articles