Search in sources :

Example 26 with InstrumentingClassLoader

use of org.evosuite.instrumentation.InstrumentingClassLoader in project evosuite by EvoSuite.

the class CBranchFitnessArchiveSystemTest method test.

@Test
public void test() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
    Properties.TARGET_CLASS = CBranchExample.class.getCanonicalName();
    Properties.CRITERION = new Criterion[] { Criterion.CBRANCH };
    Properties.TEST_ARCHIVE = true;
    InstrumentingClassLoader classLoader = TestGenerationContext.getInstance().getClassLoaderForSUT();
    Class<CBranchExample> sut = (Class<CBranchExample>) classLoader.loadClass(Properties.TARGET_CLASS);
    List<String> classpath = new ArrayList<>();
    String cp = System.getProperty("user.dir") + "/target/test-classes";
    classpath.add(cp);
    ClassPathHandler.getInstance().addElementToTargetProjectClassPath(cp);
    DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, classpath);
    Constructor<CBranchExample> ctor = sut.getConstructor();
    Method method = sut.getMethod("Subject", String.class, String.class);
    TestSuiteChromosome suiteChromosome = new TestSuiteChromosome();
    TestCaseBuilder builder = new TestCaseBuilder();
    VariableReference fileSuffix1 = builder.appendConstructor(ctor);
    VariableReference string0 = builder.appendStringPrimitive("MY}6.);:<<TtF");
    VariableReference string1 = builder.appendStringPrimitive("bin");
    builder.appendMethod(fileSuffix1, method, string0, string1);
    DefaultTestCase defaultTestCase = builder.getDefaultTestCase();
    suiteChromosome.addTest(defaultTestCase);
    CBranchSuiteFitness fitness = new CBranchSuiteFitness();
    double fitnessValue = fitness.getFitness(suiteChromosome);
    // removes covered goals due to ARCHIVE
    fitness.updateCoveredGoals();
    double fitnessValue2 = fitness.getFitness(suiteChromosome);
    assertTrue(fitnessValue2 <= fitnessValue);
}
Also used : TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) CBranchSuiteFitness(org.evosuite.coverage.cbranch.CBranchSuiteFitness) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) CBranchExample(com.examples.with.different.packagename.cbranch.CBranchExample) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Aggregations

InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)26 Test (org.junit.Test)23 TestCase (org.evosuite.testcase.TestCase)15 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)13 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)12 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)5 VariableReference (org.evosuite.testcase.variable.VariableReference)4 GenericClass (org.evosuite.utils.generic.GenericClass)3 ExtendingFile (com.examples.with.different.packagename.mock.java.io.ExtendingFile)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 NonInstrumentingClassLoader (org.evosuite.instrumentation.NonInstrumentingClassLoader)2 MockFile (org.evosuite.runtime.mock.java.io.MockFile)2 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)2 CBranchExample (com.examples.with.different.packagename.cbranch.CBranchExample)1 MemoryCheck (com.examples.with.different.packagename.mock.java.lang.MemoryCheck)1 TypeToken (com.googlecode.gentyref.TypeToken)1 File (java.io.File)1 Assertion (org.evosuite.assertion.Assertion)1