How to test enum types?

后端 未结 5 1818
灰色年华
灰色年华 2020-12-09 16:40

I\'m currently trying to build a more or less complete set of unit tests for a small library. Since we want to allow different implementations to exist we want this set of t

相关标签:
5条回答
  • 2020-12-09 17:22

    you can test if have exactly some values, by example:

    for(MyBoolean b : MyBoolean.values()) {
        switch(b) {
        case TRUE:
            break;
        case FALSE:
            break;
        default:
            throw new IllegalArgumentException(b.toString());
    }
    
    for(String s : new String[]{"TRUE", "FALSE" }) {
        MyBoolean.valueOf(s);
    }
    

    If someone removes or adds a value, some of test fails.

    0 讨论(0)
  • 2020-12-09 17:25

    I agree with aberrant80.

    For enums, I test them only when they actually have methods in them. If it's a pure value-only enum like your example, I'd say don't bother.

    But since you're keen on testing it, going with your second option is much better than the first. The problem with the first is that if you use an IDE, any renaming on the enums would also rename the ones in your test class.

    I would expand on it by adding that unit testings an Enum can be very useful. If you work in a large code base, build time starts to mount up and a unit test can be a faster way to verify functionality (tests only build their dependencies). Another really big advantage is that other developers cannot change the functionality of your code unintentionally (a huge problem with very large teams).

    And with all Test Driven Development, tests around an Enums Methods reduce the number of bugs in your code base.

    Simple Example

    public enum Multiplier {
        DOUBLE(2.0),
        TRIPLE(3.0);
    
        private final double multiplier;
    
        Multiplier(double multiplier) {
            this.multiplier = multiplier;
        }
    
        Double applyMultiplier(Double value) {
            return multiplier * value;
        }
    
    }
    
    public class MultiplierTest {
    
        @Test
        public void should() {
            assertThat(Multiplier.DOUBLE.applyMultiplier(1.0), is(2.0));
            assertThat(Multiplier.TRIPLE.applyMultiplier(1.0), is(3.0));
        }
    }
    
    0 讨论(0)
  • 2020-12-09 17:25

    Usually I would say it is overkill, but there are occasionally reasons for writing unit tests for enums.

    Sometimes the values assigned to enumeration members must never change or the loading of legacy persisted data will fail. Similarly, apparently unused members must not be deleted. Unit tests can be used to guard against a developer making changes without realising the implications.

    0 讨论(0)
  • 2020-12-09 17:40

    If you use all of the months in your code, your IDE won't let you compile, so I think you don't need unit testing.

    But if you are using them with reflection, even if you delete one month, it will compile, so it's valid to put a unit test.

    0 讨论(0)
  • 2020-12-09 17:42

    For enums, I test them only when they actually have methods in them. If it's a pure value-only enum like your example, I'd say don't bother.

    But since you're keen on testing it, going with your second option is much better than the first. The problem with the first is that if you use an IDE, any renaming on the enums would also rename the ones in your test class.

    0 讨论(0)
提交回复
热议问题