All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dstadler.commons.testing.PrivateConstructorCoverage Maven / Gradle / Ivy

There is a newer version: 1.0.0.21
Show newest version
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
	{@literal @}Test
	public void testPrivateConstructor() throws Exception {
		org.dstadler.commons.testing.PrivateConstructorCoverage.executePrivateConstructor(yourclass.class);
	}

	 
	 *
	 * @param  The type of class to cover.
	 * @param targetClass The class to cover.
	 * @return The constructed instance if needed for further testing.
	 *
	 * @throws Exception If invoking the default constructor fails for any reason.
	 */
	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