Skip to content

Smart Object Construction

Yun Lin edited this page Oct 19, 2022 · 5 revisions

This feature allows evosuite++ to automatically generate a test template which is evolved specifically to cover certain branches.

  • An Example to Compare EvoObj and EvoSuite

You can go to the test case feature.objectconstruction.testgeneration.testcase.ProjectOverallTest.testBasicRulesObj(). This is a test case for evaluating how EvoObj is different from EvoSuite on the target method feature.objectconstruction.testgeneration.example.graphcontruction.BasicRules.checkRules.BasicRules. You can set the variable aor (i.e., short for apply_object_rule) to be true to enable EvoObj function, while set aor to be false to disable EvoObj function.

In this example, EvoObj can construct a template with initial coverage of about 30%. In contrast, EvoSuite can only achieve an initial coverage of about 3%.

  • Enable/Disable Test Legitimization

Test legitimization aims to make the called target method in synthesized test template executed. Nevertheless, it is somehow expensive under certain circumstance. Thus, user can use -Dtotal_legitimization_budget=0 to disable test legitimization.

  • Debugging a SF100 Java method.

programmers can check SF100OverallTest.testBugExample() to add the project-id and method signature.

        @Test
	public void testBugExample() {
		String projectId = "83_xbus";
		String[] targetMethods = new String[]{
				"net.sf.xbus.protocol.xml.XBUSXMLMessage#synchronizeResponseFields(Lnet/sf/xbus/base/xbussystem/XBUSSystem;)V"
				};
		
		int repeatTime = 1;
		int budget = 100;
		Long seed = null;
		
		String fitnessApproach = "branch";
		
		boolean aor = true;
		CommonTestUtil.evoTestSingleMethod(projectId,  
				targetMethods, fitnessApproach, repeatTime, budget, true, 
				seed, aor, "generateMOSuite", "MOSUITE", "DynaMOSA");
		
		System.currentTimeMillis();
	}
  • Debugging Smart Object Construction.

We also provide a visualization facilities for debugging the object construction graph given branch. Given an example, as long as we set ConstructionPathSynthesizer.debuggerFolder as your customized address and the second parameter of generateCode() method to be true. An example is showed as follows:

        @Test
	public void testConstructionException() throws ClassNotFoundException, RuntimeException {
		
//		Properties.RANDOM_SEED = 1599466414837l;
		
		setup();
		
		String projectId = "84_ifx-framework";
		String className = "net.sourceforge.ifxfv3.beans.ChkOrdCanRs_TypeSequence2";
		String methodName = "equals(Ljava/lang/Object;)Z";
		int lineNumber = 97;
		
		String defaultClassPath = System.getProperty("java.class.path");
		StringBuffer buffer = new StringBuffer();
		List<String> classPaths = SFBenchmarkUtils.setupProjectProperties(projectId);
		for(String classPath: classPaths) {			
//			ClassPathHandler.getInstance().addElementToTargetProjectClassPath(classPath);
			buffer.append(File.pathSeparator + classPath);
		}
		
		String newPath = defaultClassPath + buffer.toString();
		System.setProperty("java.class.path", newPath);
		
		Properties.TARGET_CLASS = className;
		Properties.TARGET_METHOD = methodName;

		ArrayList<Branch> rankedList = buildObjectConstructionGraph4SF100(classPaths);

		Branch b = searchBranch(rankedList, lineNumber);
		System.out.println(b);
		ConstructionPathSynthesizer.debuggerFolder = "D:\\linyun\\test\\";
		generateCode(b, true, false);
	}

After running the above method, the debugger folder should contain a folder with the name of the branch analyzed, which is I87 Branch 25 IFNE L97 in this example. This folder will contain text files of the test cases generated after each node, and multiple graph images which differ in the node that is highlighted. For example referring to the screenshot below, the 2-ChkOrdCanRs_TypeSequence2#equals#28.txt file contains the initial test case generated after exploring the graph node with ID 28. The graph node with ID 28 is highlighted in the graph image I87 Branch 25 IFNE L97#28.png.

debug_folder_files

To better visualize the changes to each test case at different stages, we provide a visualization tool that can be run by executing the JUnit test StartVisualizer.startVisualizer() in Evosuite. On start, you will be prompted to choose the test case text file that you would like to start visualizing as shown in the screenshot below. This test case text file should be in the debugger branch folder, which is I87 Branch 25 IFNE L97 in this example.

visualizer_start

We chose the first test case generated, 0-initial.txt. This test case is displayed on the left, while the test case displayed on the right is the next test case mutated after exploring a certain node in the graph. This explored node has ID 28 which is highlighted in the graph at the bottom of the visualizer. You can use this visualizer to easily identify the differences Evoobj made to the test cases after exploring a node in the dataflow graph.

visualizer_first_testcase

You can click the Prev and Next buttons on the top right to look at the previous or next test cases, which will cause the test cases and graph image displayed to be modified accordingly. The File button on the top left allows you to manually pick another test case to visualize. Lastly, feel free to resize the various windows in the visualizer as the UI is responsive.