How to mock interfaces in EvoSuite tests?

141 views
Skip to first unread message

Matthew Sammut

unread,
Aug 10, 2019, 5:28:46 PM8/10/19
to EvoSuite
I have a simple toy class whose purpose is to investigate how automated unit-test generation works with EvoSuite.

The class is as follows:

package mylib;

public class ClassWithInterfaceField {
   
private InterfaceExample example;


   
private ClassWithInterfaceField(InterfaceExample example) {
     
this.example = example;
   
}


   
public int getSwag() {
     
return example.getValue(null, 20);
   
}

   
public int performSwagAndLog(int swag) {
      example
.performSwag();
     
System.out.println("hmmm");
     
return swag + 1;
   
}
}


where `InterfaceExample` is defined in a separate file as follows:

    package mylib;

public interface InterfaceExample {
void performSwag();
int getValue(InterfaceExample example, int flag);
}

EvoSuite seems to be unable to generate any tests for `ClassWithInterfaceField`.

Even when I add `-Dp_functional_mocking=1.0 -Dp_reflection_on_private=1.0 -Dreflection_start_percent=1.0 -Dfunctional_mocking_percent=1.0` to the console parameters, EvoSuite does not seem to be able to mock the `InterfaceExample` as intended, as EvoSuite's output seems to always be as follows:

package mylib;


import org.junit.Test;
import static org.junit.Assert.*;
import org.evosuite.runtime.EvoRunner;
import org.evosuite.runtime.EvoRunnerParameters;
import org.junit.runner.RunWith;


@RunWith(EvoRunner.class) @EvoRunnerParameters(mockJVMNonDeterminism = true, useVFS = true, useVNET = true, resetStaticState = true, separateClassLoader = true, useJEE = true)
public class ClassWithInterfaceField_ESTest extends ClassWithInterfaceField_ESTest_scaffolding {
   
@Test
   
public void notGeneratedAnyTest() {
       
// EvoSuite did not generate any tests
   
}
 
}


I have read online that EvoSuite has the ability to mock interfaces using Mockito. Is it true that EvoSuite supports mocking? And what do I need to do to generate tests for this class?
Message has been deleted

Michel Zerbinati

unread,
Sep 2, 2019, 7:38:33 AM9/2/19
to EvoSuite
Hi Matthew,

Have you tried to change the "private ClassWithInterfaceField(InterfaceExample example)" method to "public ClassWithInterfaceField(InterfaceExample example)" to verify how does this behave?


Reply all
Reply to author
Forward
0 new messages