org.dstadler.commons.testing.PrivateConstructorCoverage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-dost Show documentation
Show all versions of commons-dost Show documentation
Common utilities I find useful in many of my projects.
package org.dstadler.commons.testing;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
/**
* @author dominik.stadler
*
*/
public class PrivateConstructorCoverage {
/**
* Helper method for removing coverage-reports for classes with only static
* methods
*
* see for related EMMA ticket
* http://sourceforge.net/tracker/index.php?func=
* detail&aid=1173251&group_id=108932&atid=651900
*
* add this to the test case for any class that has only static methods
* where coverage reports the default constructor as not covered
*
* Template:
*
*
// helper method to get coverage of the unused constructor
@Test
public void testPrivateConstructor() throws Exception {
org.dstadler.commons.testing.PrivateConstructorCoverage.executePrivateConstructor(.class);
}
*
* @param targetClass
*/
public static T executePrivateConstructor(final Class targetClass) throws Exception {
if(Modifier.isAbstract(targetClass.getModifiers())) {
throw new IllegalArgumentException("Cannot run the private constructor for abstract classes.");
}
// get the default constructor
final Constructor c = targetClass.getDeclaredConstructor(new Class[] {});
// make it callable from the outside
c.setAccessible(true);
// call it
return c.newInstance((Object[]) null);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy