Is it a good pratice to use random value to create a test object with JUnit ? Like this :
public class MonomialTest {
private static final Random RANDOM = new Random();
private Monomial monomial;
private float coefficient;
private int exponent;
@Before
public void setUp() throws Exception {
coefficient = RANDOM.nextFloat();
exponent = RANDOM.nextInt();
monomial = new Monomial(coefficient, exponent);
}
@Test
...
...
...
}
Or should I use fixed values ?
Unit tests should be deterministic to simplify finding the fault when a test fails. Therefore they should not include random values.
Checking that certain properties hold for arbitrary values (which your tests effectively do) does make sense though: For that you can use property-based testing frameworks such as junit-quickcheck which will do the random data generation for you.
it's okay to use random, but to make sure that it's working well: print the random value so u can see it, and make sure that the result is what expected to be.
or just put a known value instead of a random one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With