+ 1

Approach to testing randomness in Java?

I have created code "airport connections" that generates simple random graphs in Java. There are 5 airports - e.g. LAX, JFK, SFO, BOS and MIA and the random graph generator creates 8 connections randomly with the following constraints: * Every airport must have at least 1 connection with other airports, * Every airport can have up to max 4 connections - i.e. connected with all airports, * The graph should be connected - i.e. it should be possible to travel from any airport to another airport with 0..4 stopovers. The app thereafter calculates the shortest path for the given airline connections network between randomly chosen two airports - source and destination airport and outputs the distance of the entire journey. Now I am having difficulties in writing unit tests for this task. So far I have always written the tests using the condition - with x inputs, assert y outputs or exceptions. Would be great to hear your thoughts on what would be your suggestions regarding the approach to unit testing in case the program output is random?

17th Nov 2021, 3:18 PM
Marko Rillo
1 Answer
+ 1
create two graphs, they should be different. Then do the same, but mock math.random and the two results graphs should be the same
19th Nov 2021, 3:00 AM
Alexey Kopyshev
Alexey Kopyshev - avatar