Solved

Junit assertCollection?

Posted on 2011-03-14
6
393 Views
Last Modified: 2012-05-11
What's the best way to unit test that 2 collections are the same.
(Not the same object, but each element value of one collection is in the other, and vice versa)
0
Comment
Question by:allelopath
  • 4
6 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

Are all objects in your collections unique?
Or you may have two equal options present in any of your two collections?

if they are unique then if the sizes of both collections are equal,
and then you just traverse one collection and check that
all elements of this coll;ection are present in the second collection then
tyour collections are the same in the sense you mentioned
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

if my question above has the anser "yes" then this method should work:

boolean areTheyEqual(ArrayList a1, ArrayList a2){
if(a1.size() != a2.size() ) return false;

for(int j=0; j<a1.size(); j++){
if(!a2.contains(a1.get(j)){
return false;
}
}
return true;

}

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
Comment Utility

I think this should work even if each collection
has identical items:

boolean areTheyEqual(ArrayList a1, ArrayList a2){
if(a1.size() != a2.size() ) return false;
for(int j =0; j<a1.size(); j++){
int count1 = 0;
for(int k=0; k<a1.size(); k++){
if(a1.get(j).equals(a1.get(k))count1++;

}
int count2 = 0;
for(int k=0; k<a2.size(); k++){
if(a1.get(j).equals(a2.get(k))count2++;

}
if(count1 != count2)return false;

}
retrurn true;

}
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:allelopath
Comment Utility
Thanks.
Looks like I could also use Apache Commons CollectionsUtils
CollectionUtils.isEqualCollection(actual, expected);
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Yes, you are right - that probably would be better.
But it is nice to invent the wheel also :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
coll1.removeAll(coll2);
boolean identical = coll1.size() == 0;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now