Skip to content
Related Articles

Related Articles

Generate Junit Test Cases Using Randoop API in Java

Improve Article
Save Article
  • Last Updated : 06 Jun, 2021
Improve Article
Save Article

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.



public class Message {
   private String message;
   public Message(String message){
      this.message = message;
   public String printMessage(){
      return message;

Step 4: Compile using javac 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:


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> 


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. 


java -classpath <location where jar file located> randoop-all-4.2.x randoop.main.Main gentests –classlist=<location of the file>


java -classpath %RANDOOP_JAR% randoop.main.Main gentests --classlist=C:\User\test1.txt


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:

OperationAction 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]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!