Mocking Java enum to add a value to test fail case

前端 未结 10 1443
死守一世寂寞
死守一世寂寞 2020-11-28 23:55

I have an enum switch more or less like this:

public static enum MyEnum {A, B}

public int foo(MyEnum value) {
    switch(value) {
        case(A):          


        
10条回答
  •  不知归路
    2020-11-29 00:43

    As you indicated in your edit, you can add the functionaliy in the enum itself. However, this might not be the best option, since it can violate the "One Responsibility" principle. Another way to achieve this is to create a static map which contains enum values as key and the functionality as value. This way, you can easily test if for any enum value you have a valid behavior by looping over all the values. It might be a bit far fetched on this example, but this is a technique I use often to map resource ids to enum values.

提交回复
热议问题