Do assert statements affect the tests that cover can generate?

I ran cover on code from a tutorial I was following.

public class Fitness {

    public static final String solution = "To be or not to be, that is the question.";
    public static final int solutionLength = solution.length();

    public static int fitness(final String candidate) {
        assert(solutionLength == candidate.length());
        int result = 0;
        for (int i = 0; i < solutionLength; i++) {
            int diff = (int)Math.signum(Math.abs(candidate.charAt(i) - solution.charAt(i)));
            result += diff;
        assert(result <= solutionLength && result >= 0);
        return solution.length() - result;

With the asserts I do not get any tests with message “Cannot generate essential test for fitness: Unable to generate test inputs not throwing a trivial exception. Diffblue Cover could not determine valid inputs for the methods under test.”.

If I remove the asserts, I get a decent test, which is exciting!


Cover uses a complex and proprietary algorithm for finding relevant inputs for the method under test. That algorithm takes into account the exceptions thrown by the method under test.

With assertions enabled, your method throws an AssertError for every input string whose length is not equal to solution.length(). The firsts guesses Cover makes do not have the expected length, and after some time (too early!) Cover is giving up. We should arguably improve Cover to produce a test for this example.

With assertions disabled the method doesn’t throw any exception, which, by chance, helps Cover to find an input.