Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit testing of complex enums

I have an enum that can present in a number of different ways. As a String, as an Integer and as a Double (different ranges), as a Vector2D and finally as the enum value itself. Here is a generalised example, the values are not representative. The actual use I have for this has a greate deal more values and methods.

 public enum Example {

    value0("Name0", 0, 0.0, 0.01707, 0.12534);        
    value1("Name1", 1, 25.0, 0.1707, 0.53434);
    value2("Name2", 2, 55.0, 0.70701, 0.23534);
    value3("Name3", 3, 65.0, 0.01707, 0.34786);
    value5("Name4", 4, 100.0, 0.01707, 0.42594);

    private final String name;
    private final int number; 
    private final double head;
    private final Vector2d pointVec;

    /**
     * Constructor invoked for each value above. 
     */
    enumExample(String name, int no, double hdg, float compX, float CompY) {
      this.name = name;
      this.number = no;
      this.head = hdg;
      this.pointVec = new Vector2d(compX, compY);
    }

    public String getName(){
       return name;        
    }

    public int getNumber() {
        return no;
    }

    public int getHead() {
        return head;
    }

    public Vector2D getVector() {
        return pointVec;
    }        

    public Example getCalcValue(int value) {
        return calcValue(getNumber(value));
    }

    /*
     * There are more methods that perform calculations on the enum's 
     * attributes.
     */
 }

In order to ensure that other classes that make use of this enum are working with a correctly functional enum. I want to have a comprehensive set of tests for it, thus ensuring that the data entry has been performed correctly and that the enum and it's associated data has not got corrupted.

Currently an example of this with 5 enum values has 31 tests. I have a need for versions of this going up to 33 enum values. Which is approx 200 tests.

I was hoping to be able to use data driven testing as this would make checking the test data by eye easier.

Does anybody have any ideas as to how to set this up for an enum? All the examples of data driven testing I've found have a simple class with one method to test.

like image 224
Richard Evans Avatar asked Mar 11 '26 01:03

Richard Evans


1 Answers

Parameterized tests are not the most beautiful pieces of code every conceived (I don't like the whole object[] stuff), but you could probably use them to do your stuff... In this example, I have two tests with expected results, but of course, you can add many parameters for multiple tests, with every test testing a certain part of the enum.

@RunWith(Parameterized.class)
public class SomeTest {

    @Parameters
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { 
                 { MyEnum.Value1, 0, "x" }, 
                 { MyEnum.Value2, 1, null }, 
                 { MyEnum.Value3, 127, "y" }, 
                 etc.
           });
    }

    private MyEnum enumValue;
    private int expectedInt;
    private String expectedString;

    // Each test set will get one set of parameters from your array
    public SomeTest(MyEnum enumValue, int expectedInt, String expectedString) {
        this.enumValue = enumValue;
        this.expectedInt = expectedInt;
        this.expectedString = expectedString;
    }

    @Test
    public void testInt() {
         // do whatever calculation you need to do on the data
         int result = this.enumValue.doSomething() * 2 - 100;  
         assertEquals(expectedInt, result);
    }

    @Test
    public void testString() {
         // do whatever calculation you need to do on the data
         String result = this.enumValue.doSomethingElse().substring(2,5);
         assertEquals(expectedString, result);
    }
}

This way you would only write one set of tests for the enum itself and then parameterize them with the actual enum values and the expected result for these values.

I suggest doing the actual calculations in the test class and not just check the values, since checking the values will lead to people copy&pasting them into your test, which will help nobody.

like image 109
Florian Schaetz Avatar answered Mar 12 '26 15:03

Florian Schaetz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!