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!
Why?