aspose file tools*
The moose likes Testing and the fly likes Unit testing methods where the data is randomly generated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Unit testing methods where the data is randomly generated" Watch "Unit testing methods where the data is randomly generated" New topic
Author

Unit testing methods where the data is randomly generated

Anand A. Parikh
Greenhorn

Joined: May 12, 2009
Posts: 21
Hello,

I have the following class where roll() generates random values of dice. All the other methods in the class depend on the values generated by roll(). How can I unit test the methods including roll() with JUnit, since the actual value is different each time roll() is called?

Thank you for helping,
Anand

Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

Hi Anand!
You can unit test roll() method by mocking the Math class. You will have to use your own implementation in place of Math.random(), but without changing any code in roll(). After providing your own random method you can test for other operations such as sort , or checking the range. You can also create your tests based on the assumptions of your roll method as it is slightly predictable.
To test other methods, you need to reduce coupling between them. You can create other helper methods for state management (in place of the private array you placed) and test them independantly.
Tim Cooke
Bartender

Joined: Mar 28, 2008
Posts: 1130
    
  59

As Sumit suggested, one solution would be to mock out the Math class so you have control over the value returned by the random() method. However as Math.random() is a static method you would have to use something like PowerMock to enable you to do this. Normal mocking frameworks like EasyMock, Mockito, JMock, etc don't support the mocking of static method calls. While this is very possible it does add a lot of bloat to your Unit Test setup.

Another solution would be to override the implementation of the roll() method in your test class. For example:

The downside of this technique is that you will need to create a second instance of Dice without overriding anything to test the roll() method.

There is no right and wrong answer here for which approach you choose to take. Go with whatever you're comfortable with and makes sense for you and your team.


Tim Driven Development
Anand A. Parikh
Greenhorn

Joined: May 12, 2009
Posts: 21
Thanks Sumit and Tim.

Using PowerMock with EasyMock, I was able to mock static method Math.random() in test class and unit test roll().
Then I created a helper method mockRoll() in test class to enable me to unit test toString() and sum().

As Tim suggested, if I changed the diceArray to package or protected access, I could override roll() in test class and unit test it that way too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unit testing methods where the data is randomly generated