Author - StudySection Post Views - 1,737 views
java

Insert dummy data using Faker library(Java)

Faker library is used to generating the test data. In automation, testing the data generating process is one of the most frustrating processes. For data-driven testing, we need a lot of data that we generate manually or get the file from the internet to fill the fake data. This wastes a lot of time in both terms creating test data. Faker allows us to model this data and create fresh dynamic values for fields every time.

In this code, we are generating the “name” and “company name” using the data faker library. Then store this data into the database directly by using the Insert query. This way we can insert bulk data into the database to test the behavior of the website.

To run this code we need to create a maven project so that we can add faker dependency to import the faker package in the project.

Steps to create a Project:

  1. Create a new maven project in the Eclipse.
  2. Create a new package and a file.
  3. Add the below-mentioned dependency in the pom file of the project:
    <!--https://mvnrepository.com/artifact/com.github.javafaker/javafaker -->
    <dependency>
    <groupId>com.github.javafaker</groupId>
    <artifactId>javafaker</artifactId>
    <version>0.16</version>
    </dependency>
  4. Now right click on the project and go to the maven.
  5. Click on the update project.
  6. Paste the below code in the created file and run the project:
    package TestingAutomation.testingfaker;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import com.github.javafaker.Faker;
    public class Testing{
    //creating faker object
    static Faker faker = new Faker();
    public static String getName() {
    //Generating the first name
    String Name = faker.name().name();
    return Name;
    }
    public static String getCompany() {
    //Generating company name
    String Company = faker.company().name();
    return Company;
    }
    public static void main(String arg[]) throws SQLException,
    ClassNotFoundException {
    //Intialialization of variables
    String url = "jdbc:mysql://127.0.0.1:3306/testing?user=root";
    //Loading the required MYSQL JDBC Driver class
    Class.forName("com.mysql.cj.jdbc.Driver");
    //Creating a connection to the database
    Connection conn = DriverManager.getConnection(url);
    // Insert 10 records in the database
    int count = 0;
    while (count < 10) { //Executing SQL query and fetching the result Statement fetchData = conn.createStatement(); // Execute the Insert command String excuteComm = "INSERT INTO testing.testing(name,company)VALUES(?,?)"; PreparedStatement preparedStmt = conn.prepareStatement(excuteComm, Statement.RETURN_GENERATED_KEYS); preparedStmt.setString(1, getName()); preparedStmt.setString(2, getCompany()); // Execute the preparedstatement preparedStmt.executeUpdate(); // Getting Id of last Inserted records in the database ResultSet result = preparedStmt.getGeneratedKeys(); int id = 0; if (result.next()) { id = result.getInt(1); } System.out.println("Last Inserted ID:" + id); //Disconnect the database fetchData.close(); count++; } } }

    Result:

    Faker library

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri