Assigning static final int in a JUnit (4.8.1) test suite

Posted by Dr. Monkey on Stack Overflow See other posts from Stack Overflow or by Dr. Monkey
Published on 2010-03-21T22:45:42Z Indexed on 2010/03/21 22:51 UTC
Read the original article Hit count: 460

Filed under:
|
|
|
|

I have a JUnit test class in which I have several static final ints that can be redefined at the top of the tester code to allow some variation in the test values. I have logic in my @BeforeClass method to ensure that the developer has entered values that won't break my tests.

I would like to improve variation further by allowing these ints to be set to (sensible) random values in the @BeforeClass method if the developer sets a boolean useRandomValues = true;. I could simply remove the final keyword to allow the random values to overwrite the initialisation values, but I have final there to ensure that these values are not inadvertently changed, as some tests rely on the consistency of these values.

  1. Can I use a constructor in a JUnit test class? Eclipse starts putting red underlines everywhere if I try to make my @BeforeClass into a constructor for the test class, and making a separate constructor doesn't seem to allow assignment to these variables (even if I leave them unassigned at their declaration);

  2. Is there another way to ensure that any attempt to change these variables after the @BeforeClass method will result in a compile-time error?

  3. Can I make something final after it has been initialised?

© Stack Overflow or respective owner

Related posts about junit4

Related posts about junit