File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes What unit test would it be? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "What unit test would it be?" Watch "What unit test would it be?" New topic
Author

What unit test would it be?

You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Hi guys,

I have a Class with an array of int values in it and a method to shuffle an array.


I want to write a unit tests which would vefiry the array before and after the shuffling are not equal. Please advice what method of JUnit library should I use. Thank you very much in advance.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What method of JUnit? assertTrue (or assertFalse) and use an array comparison. I think there's also some array compares built in to later JUnit versions, too.

Of course, it would be completely possible for a shuffled array to have the same order as the original.

Moving to the testing forum.
You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Well, OK. But what about general procedure? I can't use that approach because that is the only one array and there is no sense to compare it because the results will always be the same - arrays are equal.
Would it be OK to create a new array of the same type and size within a test class before any modifications with array and afterwards make comparisons using a copy of it?

Just in doubts is it a good practice to make any additional data in test methods...

Hope I was clear Thanks!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You Gin wrote:Would it be OK to create a new array of the same type and size within a test class before any modifications with array and afterwards make comparisons using a copy of it?

Yes, you have no choice. You must use a copy of it, using for example Arrays#copyOf.


[My Blog]
All roads lead to JavaRanch
You Gin
Ranch Hand

Joined: Jul 23, 2010
Posts: 52
Thanks, that is what I needed to know actually.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What unit test would it be?
 
Similar Threads
simple sorting algorithm
Sprites disappearing after scaling down window? Aaargh the frustration!
can't split card shuffle method into a separate class
array!
adding objects to an arraylist.