Generate Junit Test Cases Using Randoop API in Java
Last Updated :
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:
Operation | Action performed |
---|
--testjar=<filename> [+] | A jar file, all of whose classes should be tested |
--classlist=<filename> | The file that lists classes under test |
--omit-classes=<regex> [+] | Do not test classes that match regular expression <string> |
--omit-classes-file=<filename> [+] | The file containing regular expressions for methods to omit |
--testclass=<string> [+] | The binary name of a class under test |
--methodlist=<filename> | The file that lists methods under test |
--omit-methods=<regex> [+] | Do not call methods that match regular expression <string> |
--omit-methods-file=<filename> [+] | File containing regular expressions for methods to omit |
--omit-field=<string> [+] | Omit field from generated tests |
--omit-field-file=<filename> | File containing field names to omit from generated tests |
--only-test-public-members=<boolean> | Only use public members in tests [default false] |
--silently-ignore-bad-class-names=<boolean> | Ignore class names specified by the user that cannot be found [default false] |
--flaky-test-behavior=<enum> | What to do if a flaky test is generated [default OUTPUT] |
--nondeterministic-methods-to-output=<int> | Number of suspected nondeterministic methods to print [default 10] |
Similar Reads
How to Write Test Cases in Java Application using Mockito and Junit?
Mockito is an open-source testing framework used for unit testing of Java applications. It plays a vital role in developing testable applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in Unit Testing. Unit Testing is a typ
4 min read
JUnit - Writing Sample Test Cases for StudentService in Java
In many parts of projects, collections play a major role. Among that ArrayList is a user-friendly collection and many times it will be required to develop software. Let us take a sample project that involves a "Student" model that contains attributes such as studentId, studentName, courseName, and G
5 min read
JUnit Test Execution For a MongoDB Collection using Maven
MongoDB is a NoSQL versatile database and it is widely used in software industries. Wherever there is no strict relational database relationship is necessary and also if there are chances of fetching the document often, it is always good to go with MongoDB. In this article, let us see the ways of C
4 min read
SecureRandom generateSeed() method in Java with Examples
The generateSeed() method of java.security.SecureRandom class is used to return the given number of seed bytes, computed using the seed generation algorithm that this class uses to seed itself. This call may be used to seed other random number generators. Syntax:Â Â public byte[] generateSeed(int num
2 min read
JUnit Testing For MySQL Project in Java
For testing a software project, automated testing is always good and that will produce error-prone results. In the case of manual testing, there are possibilities of human errors. In this article let us take a sample project and let us see how to write JUnit test cases for it. Example Project Projec
6 min read
JUnit - Sample Test Cases for String Java Service
Always automated testing helps to overcome various bugs. It will help in the integrity of the software as well. Here in this article, let us see how to write JUnit test cases for various String-related functions like "Isogram, Panagram, Anagram" etc., In the entire lifecycle of a software project, w
5 min read
How to Test Java Application using TestNG?
TestNG is an automation testing framework widely getting used across many projects. NG means âNext Generationâ and it is influenced by JUnit and it follows the annotations (@). Â End-to-end testing is easily handled by TestNG. As a sample, let us see the testing as well as the necessity to do it via
4 min read
Java.util.Random class in Java
Random class is used to generate pseudo-random numbers in java. An instance of this class is thread-safe. The instance of this class is however cryptographically insecure. This class provides various method calls to generate different random data types such as float, double, int. Constructors: Rando
4 min read
JUnit - Writing Sample Test Cases for CutOffMarkCalculation Java Service
The quality of the software is very important. Though Unit tests and integration tests are done in the manual testing way, we cannot expect all kinds of scenarios to test. Automated testing is more helpful. In this article, how to write JUnit for the cutoff marks calculation based on caste and marks
3 min read
How to Test Java List Interface Methods using Mockito?
Mockito is an open-source testing framework used for unit testing of Java applications. It plays a vital role in developing testable applications. Mockito is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in Unit Testing. The List interface in
5 min read