Thursday, December 01, 2022

How to automate Junit test case writing and executing using Evosuite from eclipse IDE

 Generally when it comes to writing junit test case we prefer to right click on the java files and select junit option and select the available ui option from the eclipse junit plugin. But this plugin gives you only the stub that is needed for the method not the whole method code execution itself..

To do that we can use Evosuite
you can refer to the documentation from the below sites.

https://www.evosuite.org/

There are different ways to use evosuite i.e. using cmd prompt and using eclipse plugin

From CMD prompt means we need to download the jar files and then execute following commands

While recording this blog evosuite was having the latest jar version as 1.2.0

https://github.com/EvoSuite/evosuite/releases/tag/v1.2.0

https://github.com/EvoSuite/evosuite/releases/download/v1.2.0/evosuite-1.2.0.jar

and then using below command we can create the junit test cases. for more option reefer to
https://github.com/SoftwareEngineeringToolDemos/FSE-2011-EvoSuite

java -jar evosuite.jar -generateTests [options]

you can also refer to the belwo article

https://www.veracode.com/blog/research/automated-unit-test-generation-java

The second option that i prefer is to use the eclipse plugin

for that you need to add eclipse plugin using below steps.

then add following line as shown below

Note:- (You may have to de-select the option “Group items by category” to see EvoSuite listed).

As i have already installed it will show me this message.

Now update your pom.xml of the project with this values

1- To enable the use of the plugin, it needs to be configured in the pom.xml of the target project. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
<pluginManagement>
<plugins>
   <plugin>
      <groupId>org.evosuite.plugins</groupId>
      <artifactId>evosuite-maven-plugin</artifactId>
      <version>${evosuiteVersion}</version>
      <executions><execution>
         <goals> <goal> prepare </goal> </goals>
         <phase> process-test-classes </phase>
      </execution></executions>
  </plugin>
</plugins>
</pluginManagement>

2- where ${evosuiteVersion} specify the version to use

1
2
3
<properties>
   <evosuiteVersion>1.0.6</evosuiteVersion>
</properties>

3- Beside configuring the plugin, there is also the need to add the EvoSuite runtime, which is used by the generated test cases. This can be done by adding the following Maven dependency in the pom.xml:

1
2
3
4
5
6
<dependency>
  <groupId>org.evosuite</groupId>
  <artifactId>evosuite-standalone-runtime</artifactId>
  <version>${evosuiteVersion}</version>
  <scope>test</scope>
</dependency>

4- When using EvoSuite’s Java Agent, you also need to configure the surefire plugin to run an initializing listener for the EvoSuite tests. This is required for when EvoSuite tests are mixed with manually written existing tests.

1
2
3
4
5
6
7
8
9
10
11
12
13
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.17</version>
   <configuration>
     <properties>
       <property>
          <name>listener</name>
          <value>org.evosuite.runtime.InitializingListener</value>
      </property>
     </properties>
  </configuration>
</plugin>

5-EvoSuite generates JUnit files, so it requires JUnit on the classpath. EvoSuite does not add it automatically as a dependency, as to avoid conflicts with different versions. We recommend to use a recent version of JUnit, at least 4.12 or above.

1
2
3
4
5
6
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

now first execute belwo command and check you are able to compile the project
mvn compile evosuite:generate

finally execute belwo command and you will find the generated JUnit class created for you.

1
mvn -DmemoryInMB=2000 -Dcores=2 evosuite:generate evosuite:export  test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
C:\STS-Workspace\siddhuaop-example>mvn -DmemoryInMB=2000 -Dcores=2 evosuite:generate evosuite:export  test
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------< com.siddhu:siddhuaop-example >--------------------
[INFO] Building siddhuaop-example 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> evosuite-maven-plugin:1.0.6:generate (default-cli) > compile @ siddhuaop-example >>>
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ siddhuaop-example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ siddhuaop-example ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< evosuite-maven-plugin:1.0.6:generate (default-cli) < compile @ siddhuaop-example <<<
[INFO]
[INFO]
[INFO] --- evosuite-maven-plugin:1.0.6:generate (default-cli) @ siddhuaop-example ---
[INFO] Going to generate tests with EvoSuite
[INFO] Total memory: 2000mb
[INFO] Time per class: 2 minutes
[INFO] Number of used cores: 2
[INFO] Target: C:\STS-Workspace\siddhuaop-example\target\classes
[INFO] Basedir: C:\STS-Workspace\siddhuaop-example
[INFO] Started spawn process manager on port 64694
[INFO] SLF4J: Class path contains multiple SLF4J bindings.
[INFO] SLF4J: Found binding in [jar:file:/C:/Users/Siddhartha/.m2/repository/org/evosuite/evosuite-master/1.0.6/evosuite-master-1.0.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: Found binding in [jar:file:/C:/Users/Siddhartha/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
[INFO] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
[INFO] SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
[INFO] Registered remote process from /127.0.0.1:64699
[INFO] * EvoSuite 1.0.6
[INFO] Going to execute 3 jobs
[INFO] Estimated completion time: 3 minutes, by 2022-12-01T15:59:15.292
[INFO] Going to start job for: com.siddhu.aop.SiddhuaopExampleApplication. Expected to end in 90 seconds, by 2022-12-01T15:57:45.832
[INFO] Going to start job for: com.siddhu.aop.siddhucomponent. Expected to end in 90 seconds, by 2022-12-01T15:57:45.832
[INFO] Registered remote process from /127.0.0.1:64702
[INFO] Registered remote process from /127.0.0.1:64703
[INFO] Registered remote process from /127.0.0.1:64715
[INFO] Registered remote process from /127.0.0.1:64717
[INFO] Completed job. Left: 2
[INFO] Going to start job for: com.siddhu.aop.siddhuhelperaop. Expected to end in 90 seconds, by 2022-12-01T15:58:36.220
[INFO] Registered remote process from /127.0.0.1:64771
[INFO] Registered remote process from /127.0.0.1:64792
[INFO] Completed job. Left: 1
[INFO] Completed job. Left: 0
[INFO] * Updating database to com.siddhu.aop.siddhucomponent
[INFO] * Updating database to com.siddhu.aop.SiddhuaopExampleApplication
[INFO] * Updating database to com.siddhu.aop.siddhuhelperaop
[INFO] === CTG run results ===
[INFO] Removed test suites: 0
[INFO] New test suites: 3
[INFO] Stopping spawn process manager
[INFO]
[INFO] --- evosuite-maven-plugin:1.0.6:export (default-cli) @ siddhuaop-example ---
[INFO] Exporting tests
[INFO] Exported tests to C:\STS-Workspace\siddhuaop-example\src\test\java
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ siddhuaop-example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ siddhuaop-example ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ siddhuaop-example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory C:\STS-Workspace\siddhuaop-example\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ siddhuaop-example ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to C:\STS-Workspace\siddhuaop-example\target\test-classes
[INFO]
[INFO] --- evosuite-maven-plugin:1.0.6:prepare (default) @ siddhuaop-example ---
[INFO] Preparing EvoSuite tests for execution
[INFO] Analyzing test folder: C:\STS-Workspace\siddhuaop-example\target\test-classes
[INFO] Found 3 EvoSuite scaffolding files
[INFO]
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ siddhuaop-example ---
[INFO] Surefire report directory: C:\STS-Workspace\siddhuaop-example\target\surefire-reports
 
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Executing org.evosuite.runtime.InitializingListener
15:57:50.750 [main] INFO org.evosuite.runtime.agent.AgentLoader - dynamically loading javaagent
15:57:50.782 [main] INFO org.evosuite.runtime.agent.AgentLoader - Using JavaAgent in C:\Users\Siddhartha\.m2\repository\org\evosuite\evosuite-standalone-runtime\1.0.6\evosuite-standalone-runtime-1.0.6.jar
............
............
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.018 sec - in com.siddhu.aop.siddhuhelperaop_ESTest
 
Results :
 
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:00 min
[INFO] Finished at: 2022-12-01T15:58:09+05:30
[INFO] ------------------------------------------------------------------------

Finally you will be able to see all the generated Junit test case along with Junit xml files as shown below

Note:- We are using JDK jdk1.8.0_281 for this POC.

Code can be found from the below location

https://github.com/shdhumale/evosuiteexample.git

No comments: