Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It´s possible to do Database driven tests using data tables using Karate? [duplicate]

Tags:

karate

When writing my API tests with Cucumber I have implemented some step definitions that allow specifying the data that needs to exist in the database for each scenario.

Something like this:

Given I have database table "users" with data:
 | id | name   |
 | 1  | User 1 |

This will do an insert in the specified table.

Is it possible to do something like this with Karate?

Thank you.

like image 994
brpaz Avatar asked Jul 17 '26 20:07

brpaz


1 Answers

Yes it is, you will need a insertRows method in your DBUTIL file which i borrowed from @peter

you can then call the methods and pass the variable from the table

Background: 
* def config = {username: 'XXXX', password: 'XXXXX', url: 'jdbc:oracle:thin:@XXXXX.net:6236/XXXX_XXXXX', driverClassName: 'oracle.jdbc.driver.OracleDriver'}
* def DbUtil = Java.type('util.DbUtils')
* def db = new DbUtil(config)

def Value = db.insertrows(INSERT INTO sales.promotions (promotion_name,discount, start_date,expired_date)VALUES(<name>);)*

below is the example of DB file, you will need to add dependencies with maven or gradle

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>
<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc6_g</artifactId>
    <version>12.1.0.2</version>
</dependency>


package util;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;


public class DbUtils {

    private static final Logger logger = LoggerFactory.getLogger(DbUtils.class); 

    private final JdbcTemplate jdbc;

    public DbUtils(Map<String, Object> config) {
        String url = (String) config.get("url");
        String username = (String) config.get("username");
        String password = (String) config.get("password");
        String driver = (String) config.get("driverClassName");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        jdbc = new JdbcTemplate(dataSource);
        logger.info("init jdbc template: {}", url);
    }

    public Object readValue(String query) {
        return jdbc.queryForObject(query, Object.class);
    }    

    public Map<String, Object> readRow(String query) {
        return jdbc.queryForMap(query);
    }

    public List<Map<String, Object>> readRows(String query) {
        return jdbc.queryForList(query);
    }  

    public void insertRows(final String sql){
        jdbc.batchUpdate(new String[]{sql});
        }

}
like image 151
Sadiq K Avatar answered Jul 22 '26 19:07

Sadiq K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!