How to test enum types?

后端 未结 5 1832
灰色年华
灰色年华 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.

提交回复
热议问题