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?