Generate Junit Test Cases Using Randoop API in Java

06 Jun, 2021
Here we will be discussing how to generate Junit test cases using Randoop along with sample illustration and snapshots of the current instances. So basically in Development If we talk about test cases, then every developer has to write test cases manually. Which is counted in the development effort and also increases the time of the project and cost estimate. So we can reduce the time taken to write the test cases with the help of some APIs. One of which is Randoop. Java and Randoop are prerequisite requirements before we move ahead. Fundamental Knowledge is required to generate the test cases using randoop you need to know the basics of Junit to verify the results.
Working of Randoop: Randoop automatically creates Junit tests for your classes. It is a unit test generator for Java. Randoop generates unit tests using feedback-directed random test generation. This technique pseudo-randomly, but smartly, generates sequences of method/constructor invocations for the classes under test. 
Randoop typically generates two types of test cases:
Error-revealing tests that detect bugs in your current code.
Regression tests that can be used to detect the future bugs.
Running of Randoop: Now you have the downloaded jar in your machine. To run it, you have to call the main method of Randoop like randoop.main.Main
Step 1: First you have to set the environment variable of randoop-all-4.2.6.jar and.
Step 2: After setting the variable open terminal and type the line given below and if everything is configured correctly then the out will be like this.
java -classpath %RANDOOP_JAR% randoop.main.Main gentests --help
Step 3: Now, generate Test cases for java file (–testclass)
Create a sample java file to generate testcases.
In this example we use –testclass option which is use to test single class file.
Example
Java
public class Message {   private String message;     public Message(String message){      this.message = message;   }      public String printMessage(){      System.out.println(message);      return message;   }   }
Step 4: Compile using javac Message.java and it will generate Message.class file which will use by randoop to generate the test cases.
Step 5: Now open the terminal/cmd and type the command like this:
Syntax:
java -classpath <location of the class file>;<location where jar file located> randoop-all-4.2.x randoop.main.Main gentests –testclass=<Class File name> 
Example:
java -classpath C:\Users\public\Downloads\testbin;%RANDOOP_JAR% randoop.main.Main gentests --testclass=Message
After running this command all the possible test cases of the Message.class file would be listed in the new Java files which is generated by Randoop named RegressionTest0, RegressionTest0.
Generate Test cases for java files (--classlist)
Implementation: In this example, we are going to generate test cases for the list of class files which is written in a simple text file and we provide that text file as an input to randoop. 
Syntax:
java -classpath <location where jar file located> randoop-all-4.2.x randoop.main.Main gentests –classlist=<location of the file>
Example:
java -classpath %RANDOOP_JAR% randoop.main.Main gentests --classlist=C:\User\test1.txt
Output:
By now we are done with generating Junit test cases using Randoop API which was our aim goal. Still some useful set of operations are listed in the tabular format below to get a stronghold over Randoop API. They are as follows:
Java 8
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.