Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

test a class that uses random values

Tags:

java

junit

I need to test a class that use random.nextDouble() is there elegant way to provide the test these number so to be able to foresee the executed output?

like image 776
Luka Avatar asked Dec 09 '22 02:12

Luka


1 Answers

I'd recommend to embed random numbers generator into separate component and inject it into your class. Thus, you would be able to inject its mocked instance in tests. It's the most elegant solution in my opinion. You can also do some tricks with reflection and override random generator created within class body but this is not recommended at all.

like image 184
omnomnom Avatar answered Dec 31 '22 05:12

omnomnom