Search in sources :

Example 1 with Scope

use of org.evosuite.testcase.execution.Scope in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testAll_once.

@Test
public void testAll_once() throws Exception {
    TestCase tc = new DefaultTestCase();
    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("all_once", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    Assert.assertEquals(0, mockStmt.getNumParameters());
    // execute first time with default mock
    Scope scope = execute(tc);
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    List<Type> types = mockStmt.updateMockedMethods();
    Assert.assertEquals(7, types.size());
}
Also used : Type(java.lang.reflect.Type) VariableReference(org.evosuite.testcase.variable.VariableReference) Scope(org.evosuite.testcase.execution.Scope) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 2 with Scope

use of org.evosuite.testcase.execution.Scope in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testArray.

@Test
public void testArray() throws Exception {
    TestCase tc = new DefaultTestCase();
    /*
            String s = "...";
            String[] array = new String[1];
            array[0] = s;
            Foo foo = mock(Foo.class);
            getFirstInArray(foo);
         */
    final String MOCKED_VALUE = "Hello 42!!!";
    VariableReference aString = tc.addStatement(new StringPrimitiveStatement(tc, MOCKED_VALUE));
    ArrayReference mockedArray = (ArrayReference) tc.addStatement(new ArrayStatement(tc, String[].class, 1));
    ArrayIndex arrayIndex = new ArrayIndex(tc, mockedArray, 0);
    AssignmentStatement stmt = new AssignmentStatement(tc, arrayIndex, aString);
    tc.addStatement(stmt);
    FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, Foo.class, Foo.class);
    VariableReference mock = tc.addStatement(mockStmt);
    VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("getFirstInArray", 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);
    Object obj = scope.getObject(result);
    // default mock value should be null for objects/arrays
    Assert.assertNull(obj);
    // 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(String[].class, types.get(0));
    // add int variable to list of mock expected returns
    mockStmt.addMissingInputs(Arrays.asList(mockedArray));
    Assert.assertEquals(1, mockStmt.getNumParameters());
    Assert.assertTrue(mockStmt.getParameterReferences().get(0).same(mockedArray));
    // re-execute with initialized mock
    scope = execute(tc);
    String val = (String) scope.getObject(result);
    Assert.assertEquals(MOCKED_VALUE, val);
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Type(java.lang.reflect.Type) Scope(org.evosuite.testcase.execution.Scope) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 3 with Scope

use of org.evosuite.testcase.execution.Scope in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testAll_twice.

@Test
public void testAll_twice() throws Exception {
    TestCase tc = new DefaultTestCase();
    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("all_twice", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    Assert.assertEquals(0, mockStmt.getNumParameters());
    // execute first time with default mock
    Scope scope = execute(tc);
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    List<Type> types = mockStmt.updateMockedMethods();
    Assert.assertEquals(14, types.size());
}
Also used : Type(java.lang.reflect.Type) VariableReference(org.evosuite.testcase.variable.VariableReference) Scope(org.evosuite.testcase.execution.Scope) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 4 with Scope

use of org.evosuite.testcase.execution.Scope 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)

Aggregations

Type (java.lang.reflect.Type)4 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)4 TestCase (org.evosuite.testcase.TestCase)4 Scope (org.evosuite.testcase.execution.Scope)4 VariableReference (org.evosuite.testcase.variable.VariableReference)4 GenericMethod (org.evosuite.utils.generic.GenericMethod)4 Test (org.junit.Test)4 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)3 BooleanPrimitiveStatement (org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement)1 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)1 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)1 ArrayReference (org.evosuite.testcase.variable.ArrayReference)1