Emma coverage on Enum types

后端 未结 3 1892
滥情空心
滥情空心 2021-02-01 01:34

I\'m running EclEmma, the Emma plugin for Eclipse, and the coverage report shows only partial coverage for an Enum I\'ve defined, even though it shows the only value in the Enum

3条回答
  •  臣服心动
    2021-02-01 01:56

    I agree with other posters that 100% code coverage can be misguided. But I have to admit to the satisfaction of getting 100% coverage on newly written core code.

    Fortunately since all enums extend the same 'class', you can achieve your 100% with a little help from your friend reflection.

    Just add the following static method in a class for your testers to call, using [EnumTypeName].class as a parameter.

      public static void superficialEnumCodeCoverage(Class> enumClass) {
        try {
          for (Object o : (Object[])enumClass.getMethod("values").invoke(null)) {
            enumClass.getMethod("valueOf", String.class).invoke(null, o.toString());
          }
        }
        catch (Throwable e) {
          throw new RuntimeException(e);
        }
      }
    

    Assuming this static function was implemented in a class called "Shared", you would only need to include this line for each enum:

    Shared.superficialEnumCodeCoverage(UserRole.class);
    

    The key word is 'superficial'.

提交回复
热议问题