Small Open Source Java Tools and Libraries

Asserting test coverage

A good approach is to have at least one test class for every production class.

If you have classes this rule-of-thumb does not apply, you can:
  • Write a dummy test class with a comment that describes why not...
  • Use an AssertCoverage.ClassFilter to exclude the classes

Test all classes in "src/main/java":

public final void testCoverage() {
    AssertCoverage.assertEveryClassHasATest(new File("src/main/java"));

Example how to exclude all sub classes of a "BaseEntity" class and classes defined in a property file:

public final void testCoverage() {

    // Define base source code directory
    final File baseDir = new File("src/main/java");
    // Exclude all classes of a common type
    final AssertCoverage.ClassFilter filterEntities = new AssertCoverage.ClassFilter() {
        public boolean isIncludeClass(final Class<?> clasz) {
            return !BaseEntity.class.isAssignableFrom(clasz);
    // Exclude all classes defined in a property file with 
    // format "full.qualified.class.name=comment":
    // assertcoverage.ExampleExcludedClass=Already tested by integration test
    final Properties excludes = Utils4J.loadProperties(ExampleExcludedClass.class, "excludeClasses.properties");
    final Set<Object> keySet = excludes.keySet();
    final String[] fqnClassNames = keySet.toArray(new String[keySet.size()]);
    final AssertCoverage.ExcludeListClassFilter filterClasses = new AssertCoverage.ExcludeListClassFilter(fqnClassNames);
    // Create AND filter
    final AssertCoverage.AndClassFilter filter = new AssertCoverage.AndClassFilter(filterEntities, filterClasses);
    // Start test
    AssertCoverage.assertEveryClassHasATest(baseDir, filter);