我们如何为数据库测试实现Cucumber测试

时间:2019-02-19 15:57:30

标签: java cucumber database-testing

我正在尝试将Cucumber测试配置为用于数据库测试,因为我的应用程序具有一些剩余服务,并且我需要检查数据库以验证记录已更新为正确的值。

我正在使用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"

数据库连接类如下:

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

}

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

我希望我的步骤执行查询,并且数据库连接被创建为背景条件。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

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

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