How we can implement Cucumber test for database testing

I am trying to configure Cucumber test for database testing since my application have some rest services and I need to check the database in order to verify the records are updated with proper value.

I am using Postgres database. I have a feature file listed as below:

Feature: API Test

Background: Given I am connected with the database

Scenario: I want to test the database connection        
When I run the select query         
Then I should see the result as "Pranjal"

Database Connection class is as follows:

public class DatabaseConnection {

public DatabaseConnection createConnection() throws Exception {

    try {
        //Connection URL Syntax: "jdbc:postgresql://ipaddress:portnumber/db_name"       
        String dbUrl = "jdbc:postgresql://localhost:5432/test";                 

        //Database User name
        String username = "postgres";   

        //Database Password
        String password = "12345678";

        //Query to Execute      
        String query = "select * from test where no = 1;";

        //Load PostgreSQL JDBC driver       
        Class.forName("org.postgresql.Driver");         

        //Create Connection to DB       
        Connection con = DriverManager.getConnection(dbUrl,username,password);

        //Create Statement Object       
        Statement stmt = con.createStatement();                 

        // Execute the SQL Query. Store results in ResultSet        
        ResultSet rs = stmt.executeQuery(query);                            

        // While Loop to iterate through all data and print results     
        while (rs.next()){
            String myName = rs.getString(1);                                                                                           
            System.out.println(myName);     
        }       

        // closing DB Connection        
        con.close();
    } catch(SQLException e) {
      e.printStackTrace();
    }

    return new DatabaseConnection();            
}

}

I have a Runner Class which will going to execute my feature file.

I want my steps to execute the query and database connection is created as a Background condition..

Is there a way to achieve this?

Upvotes: 1

Views: 22251

Answers (1)

I am able to do it by making some changes in Database connection class and use the methods in my feature file in order to check the database connection.

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Assert;

public class DatabaseConnection extends DriverFactory {

    ReadConfigFile config = new ReadConfigFile();

    public static String dbUrl;                 
    public static String username;  
    public static String password;
    public static String database_driver;
    Connection con;
    Statement stmt;
    String query;
    ResultSet rs;

    public DatabaseConnection() {
        super();
    }

    public DatabaseConnection createConnection() throws SQLException, ClassNotFoundException {
        con = DriverManager.getConnection(config.getUrl(),config.getUsername(),config.getPassword());
        Class.forName(config.getDatabaseDriver());  
        return new DatabaseConnection();
    }

    public DatabaseConnection createQuery() throws SQLException {
        query = "select * from test where no = 1;";
        stmt = con.createStatement();   
        return new DatabaseConnection();
    }

    public DatabaseConnection executeQuery() throws SQLException {
        rs = stmt.executeQuery(query);
        return new DatabaseConnection();
    }

    public DatabaseConnection assertRecords(String name) throws SQLException {
        while (rs.next()){
            String myName = rs.getString(2);                                                                                           
            Assert.assertEquals(myName,name);
        }
        return new DatabaseConnection();
    }

    public DatabaseConnection closeConnection() throws SQLException {
        con.close();
        return new DatabaseConnection();
    }
}

and then use the methods for the implementation of the steps.

package stepDefinitions;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import util.DatabaseConnection;

public class TransactionGeneratorTest extends DatabaseConnection {

    @Given("^I am connected with the database$")
    public void i_am_connected_with_the_database() throws Throwable {
        createConnection();
    }

    @When("^I run the select query$")
    public void i_run_the_select_query() throws Throwable {
        createQuery();
        executeQuery();
    }   

    @Then("^I should see the result as \"([^\"]*)\"$")
    public void i_should_see_the_result_as(String name) throws Throwable {
        assertRecords(name);
    }
}

Upvotes: 4

Related Questions