Menu

#77 Option to disable coverage for private parameterless ctor

open
nobody
None
5
2005-03-30
2005-03-30
Anonymous
No

Utility classes often have a private parameterless
constructor which is never called - it's only there to
prevent a public one being generated.

It would be helpful if private parameterless
constructors with no code could be excluded from coverage.

Discussion

  • Tom Parker

    Tom Parker - 2005-04-24

    Logged In: YES
    user_id=1037926

    This really isn't that hard to get around.

    public void testSerializableUtilitiesPrivateConstructor() {
    //For emma
    ReflectionUtilities.executePrivateConstructor(LineUtilities.class);
    }

    And have this in ReflectionUtilities (which is code in a
    package you are not testing for code coverage)

    public static void executePrivateConstructor(Class
    targetClass) {
    try {
    Constructor c =
    targetClass.getDeclaredConstructor(new Class[] {});
    c.setAccessible(true);
    c.newInstance(null);
    } catch (SecurityException e) {
    throw new ReflectionException(e);
    } catch (NoSuchMethodException e) {
    throw new ReflectionException(e);
    } catch (IllegalArgumentException e) {
    throw new ReflectionException(e);
    } catch (InstantiationException e) {
    throw new ReflectionException(e);
    } catch (IllegalAccessException e) {
    throw new ReflectionException(e);
    } catch (InvocationTargetException e) {
    throw new ReflectionException(e);
    }
    }

    In general, I think this would be difficult to get correct.
    Consider some of the items you need to work around:

    Utility classes should be required to be a final class to
    have this automatically tag as safe to ignore. Also, the
    class can only have static methods. In order to avoid
    'falsely' tagging a typesafe enumeration, one needs to
    ignore any classes where the constructor is actually used.
    Otherwise, it's proper to check it and count it as covered.

    public final class NotAUtilityClass {
    public static final NotAUtilityClass THING = new
    NotAUtilityClass ();

    private NotAUtilityClass () {
    //No work to do, as this is a typesafe enumeration
    }
    }

    My 2c anyway.

     

Log in to post a comment.