我正在尝试为数据库测试配置 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"
数据库连接类如下:
公共类数据库连接 {
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 类,它将执行我的功能文件。
我希望我执行查询的步骤和数据库连接被创建为背景条件..
有没有办法做到这一点?