Search in sources :

Example 11 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testBase.

@Test
public void testBase() throws Exception {
    TestCase tc = new DefaultTestCase();
    final int MOCKED_VALUE = 42;
    VariableReference mockedInput = tc.addStatement(new IntPrimitiveStatement(tc, MOCKED_VALUE));
    VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
    FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
    VariableReference mock = tc.addStatement(mockStmt);
    VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("base", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
    // if not executed, should be no way to tell if needs new inputs
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    Assert.assertEquals(0, mockStmt.getNumParameters());
    // execute first time with default mock
    Scope scope = execute(tc);
    Integer val = (Integer) scope.getObject(result);
    // default mock value should be 0
    Assert.assertEquals(0, val.intValue());
    // after execution, there should be one variable to provide
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    List<Type> types = mockStmt.updateMockedMethods();
    Assert.assertEquals(1, types.size());
    Assert.assertEquals(int.class, types.get(0));
    // add int variable to list of mock expected returns
    mockStmt.addMissingInputs(Arrays.asList(mockedInput));
    Assert.assertEquals(1, mockStmt.getNumParameters());
    Assert.assertTrue(mockStmt.getParameterReferences().get(0).same(mockedInput));
    // re-execute with initialized mock
    scope = new Scope();
    for (Statement st : tc) {
        st.execute(scope, System.out);
    }
    val = (Integer) scope.getObject(result);
    Assert.assertEquals(MOCKED_VALUE, val.intValue());
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Type(java.lang.reflect.Type) Scope(org.evosuite.testcase.execution.Scope) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) Test(org.junit.Test)

Example 12 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testPackageLevel_local.

@Test
public void testPackageLevel_local() throws Exception {
    TestCase tc = new DefaultTestCase();
    VariableReference ref = new VariableReferenceImpl(tc, PackageLevel.class);
    try {
        FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, PackageLevel.class);
        fail();
    } catch (java.lang.IllegalArgumentException e) {
    // expected
    }
// tc.addStatement(mockStmt);
// execute(tc);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 13 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testPackageLevel_differentPackage.

@Test
public void testPackageLevel_differentPackage() throws Exception {
    TestCase tc = new DefaultTestCase();
    Class<?> example = Class.forName("com.examples.with.different.packagename.fm.ExamplePackageLevel");
    VariableReference ref = new VariableReferenceImpl(tc, example);
    try {
        FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, example);
        fail();
    } catch (java.lang.IllegalArgumentException e) {
    // expected
    }
// tc.addStatement(mockStmt);
// execute(tc);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 14 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testPackageLevel_differentPackage_nonInstrumentation_package.

@Test
public void testPackageLevel_differentPackage_nonInstrumentation_package() throws Exception {
    TestCase tc = new DefaultTestCase();
    ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
    NonInstrumentingClassLoader loader = new NonInstrumentingClassLoader();
    Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePackageLevel");
    VariableReference ref = new VariableReferenceImpl(tc, example);
    try {
        FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, example);
        fail();
    } catch (java.lang.IllegalArgumentException e) {
    // expected
    }
// tc.addStatement(mockStmt);
// execute(tc);
}
Also used : NonInstrumentingClassLoader(org.evosuite.instrumentation.NonInstrumentingClassLoader) VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 15 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class TestOverloading method testNotOverloadedMethod.

@Test
public void testNotOverloadedMethod() {
    Class<?> clazz = ClassWithoutOverloadedMethods.class;
    Method method1 = clazz.getMethods()[0];
    Method method2 = clazz.getMethods()[1];
    GenericMethod genericMethod1 = new GenericMethod(method1, clazz);
    GenericMethod genericMethod2 = new GenericMethod(method2, clazz);
    TestCase test = new DefaultTestCase();
    ConstantValue intValue = new ConstantValue(test, int.class);
    VariableReference stringVar = new VariableReferenceImpl(test, String.class);
    List<VariableReference> parameters1 = Arrays.asList(intValue);
    List<VariableReference> parameters2 = Arrays.asList(stringVar);
    assertFalse(genericMethod1.isOverloaded());
    assertFalse(genericMethod2.isOverloaded());
    assertFalse(genericMethod1.isOverloaded(parameters1));
    assertFalse(genericMethod2.isOverloaded(parameters1));
    assertFalse(genericMethod1.isOverloaded(parameters2));
    assertFalse(genericMethod2.isOverloaded(parameters2));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) Method(java.lang.reflect.Method) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) ConstantValue(org.evosuite.testcase.variable.ConstantValue) ClassWithoutOverloadedMethods(com.examples.with.different.packagename.utils.generic.ClassWithoutOverloadedMethods) Test(org.junit.Test)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 TestCase (org.evosuite.testcase.TestCase)14 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)13 Test (org.junit.Test)13 GenericMethod (org.evosuite.utils.generic.GenericMethod)5 Type (java.lang.reflect.Type)4 ConstantValue (org.evosuite.testcase.variable.ConstantValue)4 Method (java.lang.reflect.Method)3 NonInstrumentingClassLoader (org.evosuite.instrumentation.NonInstrumentingClassLoader)3 Scope (org.evosuite.testcase.execution.Scope)3 ClassWithOverloadedMethods (com.examples.with.different.packagename.utils.generic.ClassWithOverloadedMethods)2 ClassWithoutOverloadedMethods (com.examples.with.different.packagename.utils.generic.ClassWithoutOverloadedMethods)2 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)2 BooleanPrimitiveStatement (org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement)2 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)2 EqualsAssertion (org.evosuite.assertion.EqualsAssertion)1 MethodStatement (org.evosuite.testcase.statements.MethodStatement)1 PrimitiveExpression (org.evosuite.testcase.statements.PrimitiveExpression)1 Statement (org.evosuite.testcase.statements.Statement)1