Friday, December 01, 2023

Simplify Your Testing with EasyRandom

 EasyRandom is a Java library that simplifies the generation of random objects. It is a powerful tool for developers working with object-oriented programming languages like Java. EasyRandom allows developers to easily generate random objects, which can be beneficial for testing, prototyping, and data seeding. By eliminating the need for writing boilerplate code, EasyRandom saves time and effort. With just a few lines of code, EasyRandom can generate objects with random values for all their fields. This means that developers can focus on testing or building their application logic, rather than spending time creating random data.

During unit testing, developers often need to create numerous model classes with hardcoded values. This process can be cumbersome, as it involves creating different constructors and setting their values. Making any changes to the constructor or other dependent classes used within the model can potentially break all the other JUnit classes that have already been implemented. To avoid such issues and ensure that previously implemented code in JUnit remains intact, EasyRandom provides an ideal solution. By simplifying the creation of model class objects with random or well-defined set values, EasyRandom streamlines the testing process.

If someone ask you which is better way to create filled object

1
2
3
Street street = new Street(12, (byte) 1, "Oxford street");
Address address = new Address(street, "123456", "London", "United Kingdom");
Person person = new Person("Foo", "Bar", "foo.bar@gmail.com", Gender.MALE, address);

or

1
2
EasyRandom easyRandom = new EasyRandom();
Person person = easyRandom.nextObject(Person.class);

I am sure most of the developer which chose to use the EasyRandom options.

How to start with EasyRandom. We just need to add its dependencies in our POM xls and that its and we are good to use it.
So lets take first ready made helloworld java project i am using maven project.

and les import that project in our STS using belwo screen

Now just enter out EasyRandom maven entry in pom.xml.

1
2
3
4
5
6
<dependency>
    <groupId>org.jeasy</groupId>
    <artifactId>easy-random-core</artifactId>
    <version>5.0.0</version>
</dependency>

Now lets create a three model class
1- Street

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
package com.test.project.MavenEclipseProject;
 
public class Street {
     
    String streetName;
    String StreetNumber;
     
 
    @Override
    public String toString() {
        return "Street [streetName=" + streetName + ", StreetNumber=" + StreetNumber + "]";
    }
 
 
    /**
     * @return the streetName
     */
    public String getStreetName() {
        return streetName;
    }
 
 
    /**
     * @param streetName the streetName to set
     */
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }
 
 
    /**
     * @return the streetNumber
     */
    public String getStreetNumber() {
        return StreetNumber;
    }
 
 
    /**
     * @param streetNumber the streetNumber to set
     */
    public void setStreetNumber(String streetNumber) {
        StreetNumber = streetNumber;
    }
 
 
    public Street() {
        // TODO Auto-generated constructor stub
    }
 
}

2- Address

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
package com.test.project.MavenEclipseProject;
 
public class Address {
     
    String societyName;
    Street objStreet;
     
 
    @Override
    public String toString() {
        return "Address [societyName=" + societyName + ", objStreet=" + objStreet + "]";
    }
 
 
    /**
     * @return the societyName
     */
    public String getSocietyName() {
        return societyName;
    }
 
 
    /**
     * @param societyName the societyName to set
     */
    public void setSocietyName(String societyName) {
        this.societyName = societyName;
    }
 
 
    /**
     * @return the objStreet
     */
    public Street getObjStreet() {
        return objStreet;
    }
 
 
    /**
     * @param objStreet the objStreet to set
     */
    public void setObjStreet(Street objStreet) {
        this.objStreet = objStreet;
    }
 
 
    public Address() {
        // TODO Auto-generated constructor stub
    }
 
}

3- Person

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
package com.test.project.MavenEclipseProject;
 
public class Person {
     
    String personName;
    Address objAddress;
 
    @Override
    public String toString() {
        return "Person [personName=" + personName + ", objAddress=" + objAddress + "]";
    }
 
    /**
     * @return the personName
     */
    public String getPersonName() {
        return personName;
    }
 
    /**
     * @param personName the personName to set
     */
    public void setPersonName(String personName) {
        this.personName = personName;
    }
 
    /**
     * @return the objAddress
     */
    public Address getObjAddress() {
        return objAddress;
    }
 
    /**
     * @param objAddress the objAddress to set
     */
    public void setObjAddress(Address objAddress) {
        this.objAddress = objAddress;
    }
 
    public Person() {
        // TODO Auto-generated constructor stub
    }
 
}

We will and create the instance of person using easyrandom in our app.class as shown below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.test.project.MavenEclipseProject;
 
import org.jeasy.random.EasyRandom;
 
/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args )
    {
        System.out.println( "Showing usage of EasyRandom" );
 
        for (int i=0;i<5;i++)
        {
 
            EasyRandom easyRandom = new EasyRandom();
            Person person = easyRandom.nextObject(Person.class);
            System.out.println("Value of i is:"+i+" and person object values is:" + person );
        }
 
    }
}

Lets run the class and you will find below values as output.

1
2
3
4
5
6
Showing usage of EasyRandom
Value of i is:0 and person object values is:Person [personName=eOMtThyhVNLWUZNRcBaQKxI, objAddress=Address [societyName=yedUsFwdkelQbxeTeQOvaScfqIOOmaa, objStreet=Street [streetName=JxkyvRnL, StreetNumber=RYtGKbgicZaHCBRQDSx]]]
Value of i is:1 and person object values is:Person [personName=eOMtThyhVNLWUZNRcBaQKxI, objAddress=Address [societyName=yedUsFwdkelQbxeTeQOvaScfqIOOmaa, objStreet=Street [streetName=JxkyvRnL, StreetNumber=RYtGKbgicZaHCBRQDSx]]]
Value of i is:2 and person object values is:Person [personName=eOMtThyhVNLWUZNRcBaQKxI, objAddress=Address [societyName=yedUsFwdkelQbxeTeQOvaScfqIOOmaa, objStreet=Street [streetName=JxkyvRnL, StreetNumber=RYtGKbgicZaHCBRQDSx]]]
Value of i is:3 and person object values is:Person [personName=eOMtThyhVNLWUZNRcBaQKxI, objAddress=Address [societyName=yedUsFwdkelQbxeTeQOvaScfqIOOmaa, objStreet=Street [streetName=JxkyvRnL, StreetNumber=RYtGKbgicZaHCBRQDSx]]]
Value of i is:4 and person object values is:Person [personName=eOMtThyhVNLWUZNRcBaQKxI, objAddress=Address [societyName=yedUsFwdkelQbxeTeQOvaScfqIOOmaa, objStreet=Street [streetName=JxkyvRnL, StreetNumber=RYtGKbgicZaHCBRQDSx]]]

So in short EasyRandom is a Java library that simplifies the process of creating random objects for testing purposes. When it comes to JUnit testing, using EasyRandom can offer several benefits:

Simplified Test Data Creation: EasyRandom provides a convenient way to generate random and diverse test data, reducing the need for manually crafting test objects. This can make your test code more concise and easier to maintain.

Increased Test Coverage: By easily generating a variety of test scenarios with random data, you can increase the coverage of your tests. This helps identify potential issues or edge cases that might not be apparent when using fixed or hardcoded values.

Time and Effort Savings: Manually creating test objects with specific values can be time-consuming. EasyRandom automates this process, saving time and effort during the test data setup phase.

Reduced Test Code Boilerplate: Using EasyRandom can lead to more concise and readable test code, as you won’t need extensive object creation logic. This can make your tests more focused and easier to understand.

Support for Complex Objects: EasyRandom can handle the creation of complex objects with nested structures, making it suitable for scenarios where your test data involves intricate relationships between objects.

Customization Options: While EasyRandom can generate random values automatically, it also allows for customization. You can specify certain constraints, provide your own generators, or even use annotated classes to guide the randomization process.

Improved Test Object Consistency: EasyRandom ensures that each time you generate test data, it is random but consistent. This can be useful for scenarios where you want the randomness but also need a level of predictability for assertions in your tests.

Overall, EasyRandom can enhance the efficiency and effectiveness of your JUnit tests by simplifying the process of creating diverse and randomized test data. It’s particularly useful in situations where having a wide range of test cases is important for thorough testing.

No comments: