Java 如何为数据库测试实现Cucumber测试

Java 如何为数据库测试实现Cucumber测试,java,cucumber,database-testing,Java,Cucumber,Database Testing,我正在尝试为数据库测试配置Cucumber测试,因为我的应用程序有一些rest服务,我需要检查数据库,以验证记录是否用正确的值更新 我正在使用Postgres数据库。我有一个功能文件,如下所示: Feature: API Test Background: Given I am connected with the database Scenario: I want to test the database connection When I run the select qu

我正在尝试为数据库测试配置Cucumber测试,因为我的应用程序有一些rest服务,我需要检查数据库,以验证记录是否用正确的值更新

我正在使用Postgres数据库。我有一个功能文件,如下所示:

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"
数据库连接类如下所示:

公共类数据库连接{

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();            
}
}

我有一个Runner类,它将执行我的功能文件

我希望执行查询的步骤和数据库连接作为后台条件创建


有没有办法做到这一点?

我可以通过在数据库连接类中进行一些更改并使用功能文件中的方法来检查数据库连接来做到这一点

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();
    }
}
然后使用该方法进行步骤的实现

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);
    }
}

我可以通过在数据库连接类中进行一些更改,并使用功能文件中的方法来检查数据库连接

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();
    }
}
然后使用该方法进行步骤的实现

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);
    }
}