Search in sources :

Example 1 with FunctionalMockStatement

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;
}
Also used : Type(java.lang.reflect.Type) VariableReference(org.evosuite.testcase.variable.VariableReference) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) Statement(org.evosuite.testcase.statements.Statement) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 2 with FunctionalMockStatement

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");
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement) Statement(org.evosuite.testcase.statements.Statement) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement)

Example 3 with FunctionalMockStatement

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);
    }
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) NullReference(org.evosuite.testcase.variable.NullReference) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement)

Example 4 with FunctionalMockStatement

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;
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) Statement(org.evosuite.testcase.statements.Statement) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement)

Aggregations

FunctionalMockStatement (org.evosuite.testcase.statements.FunctionalMockStatement)4 Statement (org.evosuite.testcase.statements.Statement)3 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)2 VariableReference (org.evosuite.testcase.variable.VariableReference)2 GenericClass (org.evosuite.utils.generic.GenericClass)2 Type (java.lang.reflect.Type)1 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1 PrivateFieldStatement (org.evosuite.testcase.statements.reflection.PrivateFieldStatement)1 PrivateMethodStatement (org.evosuite.testcase.statements.reflection.PrivateMethodStatement)1 NullReference (org.evosuite.testcase.variable.NullReference)1