• Post Reply Bookmark Topic Watch Topic
  • New Topic

JUnit Testing  RSS feed

 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a method I wrote that removes a random element from an ArrayList and returns that element. Does anyone have any good ideas on how to test this? I can write the tests. I just don't know how to test something that is random? Thanks a bunch. All you at the ranch are always helpful.

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would test that the size of "bag" is one smaller after calling the method than it was before calling the method. That's all that your method guarantees.
 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Greg Charles

Thank you. Would you say that is actually is random? Does the method appear to work as far as you can tell?
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Koch wrote: Would you say that is actually is random? Does the method appear to work as far as you can tell?

From Oracle docs with regard to the Random class:
nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.


So sure, it should work.

you should probably declare Random in the method though. Keep the declaration near the rest of its code. Any time you need help it helps the reader/helper. The snippet is pretty short so we don't know what "bag" is but we'll take your word for it.

And unless the method needs to be public (dangerous) then make it private or protected. In general never make a method public unless you need to expose it to a caller outside of the package.

Anyway, you should just Assert to see if the ArrayList is equal to the old size or not. It should work.
 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul

Here is the whole class

 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Koch wrote:@Paul

Here is the whole class


That will compile but it won't run, William. You need to initialize randomGenerator with "new" (like you did with the ArrayList) or you'll get a NullPointerException at runtime.

Other than that it should work. Just put an Assert or other value checker in the remove method that checks the size before and after the remove operation. Good to go.

p
 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch, yeah when I did a JUnit Test, I tried a System.out.println() to check the random numbers that were generating and it wouldn't print. It is because I forgot to add the "new" i fixed it though by deciding to just using the following line to get a random number.

int rnd = (int)(Math.random() * bag.size());

Thanks a bunch!
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Koch wrote:Thanks a bunch, yeah when I did a JUnit Test, I tried a System.out.println() to check the random numbers that were generating and it wouldn't print. It is because I forgot to add the "new" i fixed it though by deciding to just using the following line to get a random number.

int rnd = (int)(Math.random() * bag.size());

Thanks a bunch!

Good going, lots of different ways to solve the puzzle.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Witten wrote: . . . You should probably declare Random in the method though. . . .
Are you sure about that? You would usually declare such objects as local variables, but in the case of Random there is a risk of two Random objects giving the same sequence of numbers (it was worse in jdks ≤ 1.4.2), so having a Random object as a field might be appropriate.

Anybody else?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Koch wrote: . . . check the random numbers that were generating and it wouldn't print. It is because I forgot to add the "new" i fixed it though by deciding to just using the following line to get a random number.

int rnd = (int)(Math.random() * bag.size());

. . .
Not a good way to sort out a test; you ought to have found why you were not getting a printout; in fact you would have suffered a NullPointerException.
Math.random() uses a random object anyway, so you haven’t really changed anything. I think it always uses the same Random object, so the problem I mentioned earlier cannot occur.
 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Had there been a method:



in RandomPool<E> class, We could write unit test like this:

 
William Koch
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone I did get it all working. Here are my results in the following order. I still need to add the test that tests size. But other than that everything appears to work, it compiles, and it has run correctly.

1) the Pool Interface
2)The RandomPool Class
3) RandomPoolTest case.





 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!