When I was learning to use Evosuite, I wrote some wrong methods. In one of them, for example, a method of class A
directly calls an abstract method of interface B
, and EvoSuite generated a mock for B
to avoid the case failing:
import org.junit.Test;
import static org.junit.Assert.*;
import static org.evosuite.shaded.org.mockito.Mockito.*;
import MockTest.A;
import MockTest.B;
import org.evosuite.runtime.EvoRunner;
import org.evosuite.runtime.EvoRunnerParameters;
import org.evosuite.runtime.ViolatedAssumptionAnswer;
import org.junit.runner.RunWith;
@RunWith(EvoRunner.class) @EvoRunnerParameters(useVNET = true, separateClassLoader = true, useJEE = true)
public class A_ESTest extends A_ESTest_scaffolding {
@Test(timeout = 4000)
public void test0() throws Throwable {
A a0 = new A();
B b0 = mock(B.class, new ViolatedAssumptionAnswer());
doReturn((String) null).when(b0).get(anyInt());
String string0 = a0.test(b0);
assertNull(string0);
}
}
For a method where a divide by zero may occur, the EvoSuite-generated test case even catches an exception to ensure that the test case passes:
import org.junit.Test;
import static org.junit.Assert.*;
import static org.evosuite.runtime.EvoAssertions.*;
import org.evosuite.runtime.EvoRunner;
import org.evosuite.runtime.EvoRunnerParameters;
import org.junit.runner.RunWith;
import test.Case2;
@RunWith(EvoRunner.class) @EvoRunnerParameters(useVNET = true, separateClassLoader = true, useJEE = true)
public class Case2_ESTest extends Case2_ESTest_scaffolding {
@Test(timeout = 4000)
public void test8() throws Throwable {
Case2 case2_0 = new Case2();
// Undeclared exception!
try {
case2_0.add(6, 0);
fail("Expecting exception: ArithmeticException");
} catch(ArithmeticException e) {
//
// / by zero
//
verifyException("test.Case2", e);
}
}
}
Are EvoSuite's automatically generated test cases always passing, as in these examples? How can EvoSuite be made to generate test cases that can fail, and thus can be directly used by developers?