use of org.evosuite.testcase.statements.FunctionalMockStatement in project evosuite by EvoSuite.
the class TestChromosome method mockChange.
private boolean mockChange() {
/*
Be sure to update the mocked values if there has been any change in
behavior in the last execution.
Note: mock "expansion" cannot be done after a test has been mutated and executed,
as the expansion itself might have side effects. Therefore, it has to be done
before a test is evaluated.
*/
boolean changed = false;
for (int i = 0; i < test.size(); i++) {
Statement st = test.getStatement(i);
if (!(st instanceof FunctionalMockStatement)) {
continue;
}
FunctionalMockStatement fms = (FunctionalMockStatement) st;
if (!fms.doesNeedToUpdateInputs()) {
continue;
}
int preLength = test.size();
try {
List<Type> missing = fms.updateMockedMethods();
int pos = st.getPosition();
logger.debug("Generating parameters for mock call");
// Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
List<VariableReference> refs = TestFactory.getInstance().satisfyParameters(test, null, missing, null, pos, 0, true, false, true);
fms.addMissingInputs(refs);
} catch (Exception e) {
// shouldn't really happen because, in the worst case, we could create mocks for missing parameters
String msg = "Functional mock problem: " + e.toString();
AtMostOnceLogger.warn(logger, msg);
fms.fillWithNullRefs();
return changed;
}
changed = true;
int increase = test.size() - preLength;
i += increase;
}
return changed;
}
use of org.evosuite.testcase.statements.FunctionalMockStatement in project evosuite by EvoSuite.
the class Scaffolding method generateMockInitialization.
/**
* This is needed because the first time we do initialize a mock object, that can take
* some seconds (successive calls would be based on cached data), and so tests might
* timeout. So here we force the mock initialization in a @BeforeClass
*
* @param bd
* @param results
*/
private void generateMockInitialization(String testClassName, StringBuilder bd, List<ExecutionResult> results) {
if (!TestSuiteWriterUtils.doesUseMocks(results)) {
return;
}
// In order to make sure this is called *after* initializeClasses this method is now called directly from initEvoSuiteFramework
// bd.append(METHOD_SPACE);
// bd.append("@BeforeClass \n");
bd.append(METHOD_SPACE);
bd.append("private static void initMocksToAvoidTimeoutsInTheTests() throws ClassNotFoundException { \n");
Set<String> mockStatements = new LinkedHashSet<>();
for (ExecutionResult er : results) {
for (Statement st : er.test) {
if (st instanceof FunctionalMockStatement) {
FunctionalMockStatement fms = (FunctionalMockStatement) st;
String name = new GenericClass(fms.getReturnType()).getRawClass().getTypeName();
mockStatements.add("mock(Class.forName(\"" + name + "\", false, " + testClassName + ".class.getClassLoader()));");
}
}
}
mockStatements.stream().sorted().forEach(m -> {
bd.append(BLOCK_SPACE);
bd.append(m);
bd.append("\n");
});
bd.append(METHOD_SPACE);
bd.append("}\n");
}
use of org.evosuite.testcase.statements.FunctionalMockStatement in project evosuite by EvoSuite.
the class RandomInsertion method selectRandomVariableForCall.
private VariableReference selectRandomVariableForCall(TestCase test, int position) {
if (test.isEmpty() || position == 0)
return null;
List<VariableReference> allVariables = test.getObjects(position);
List<VariableReference> candidateVariables = new ArrayList<>();
for (VariableReference var : allVariables) {
if (!(var instanceof NullReference) && !var.isVoid() && !var.getGenericClass().isObject() && !(test.getStatement(var.getStPosition()) instanceof PrimitiveStatement) && !var.isPrimitive() && (test.hasReferences(var) || var.getVariableClass().equals(Properties.getInitializedTargetClass())) && // do not directly call methods on mock objects
!(test.getStatement(var.getStPosition()) instanceof FunctionalMockStatement)) {
candidateVariables.add(var);
}
}
if (candidateVariables.isEmpty()) {
return null;
} else if (Properties.SORT_OBJECTS) {
candidateVariables = candidateVariables.stream().sorted(Comparator.comparingInt(item -> item.getDistance())).collect(Collectors.toList());
return ListUtil.selectRankBiased(candidateVariables);
} else {
return Randomness.choice(candidateVariables);
}
}
use of org.evosuite.testcase.statements.FunctionalMockStatement in project evosuite by EvoSuite.
the class Archive method hasFunctionalMocksForGenerableTypes.
private boolean hasFunctionalMocksForGenerableTypes(TestCase testCase) {
for (Statement statement : testCase) {
if (statement instanceof FunctionalMockStatement) {
FunctionalMockStatement fm = (FunctionalMockStatement) statement;
Class<?> target = fm.getTargetClass();
GenericClass gc = new GenericClass(target);
if (TestCluster.getInstance().hasGenerator(gc)) {
return true;
}
}
}
return false;
}
Aggregations