Wednesday, September 17, 2025

java – Take a look at knowledge administration for automation api testing


You may create a CSV file and put your take a look at knowledge in that file. Now, you possibly can learn the information from the CSV file and use it in your take a look at case.

Create a CSV file along with your take a look at knowledge. I’m utilizing the next for instance:

Identify,Emp Id,Division
abc,123,xyz

You may modify the information in your CSV file in keeping with your take a look at necessities.

Use the next code as a reference:

public class YourTestClass {

    @ParameterizedTest
    @CsvFileSource(sources = "/your_data_file.csv", numLinesToSkip = 1)
    // Assuming your CSV file is within the sources listing and the primary line comprises headers
    
    public void testMethod(String identify, int empId, String division) {
        System.out.println("Identify: " + identify + ", Emp Id: " + empId + ", Division: " + division);
        // Add your assertions or take a look at logic right here
        // For instance:
        assertEquals("xyz", division);
    }
}

Alternatively, you probably have a big checklist of take a look at knowledge, you should utilize a CSV parsing library to learn the information from the file and retailer it in a map. Under is an instance:

import com.opencsv.CSVReader;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class TestDataLoader {

    public static Map loadTestData(String filePath) {
        Map testDataMap = new HashMap<>();

        attempt (CSVReader reader = new CSVReader(new FileReader(filePath))) {
            String[] headers = reader.readNext(); // Assuming the primary line comprises headers

            String[] nextLine;
            int index = 0;
            whereas ((nextLine = reader.readNext()) != null) {
                Object[] rowData = new Object[headers.length];
                for (int i = 0; i < headers.size; i++) {
                    rowData[i] = nextLine[i];
                }
                testDataMap.put("Take a look at" + index++, rowData);
            }
        } catch (IOException e) {
            e.printStackTrace();
            // Deal with the exception in keeping with your necessities
        }

        return testDataMap;
    }
}

Use the map in your JUnit take a look at class:

import org.junit.Take a look at;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Map;

import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class YourTestClass {

    non-public String identify;
    non-public int empId;
    non-public String division;

    // Constructor to obtain the parameters
    public YourTestClass(String identify, int empId, String division) {
        this.identify = identify;
        this.empId = empId;
        this.division = division;
    }

    @Take a look at
    public void testMethod() {
        // Your take a look at logic utilizing the parameters
        System.out.println("Identify: " + identify + ", Emp Id: " + empId + ", Division: " + division);

        // Add your assertions or take a look at logic right here
        // For instance:
        assertEquals("xyz", division);
    }

    @Parameterized.Parameters
    public static Map knowledge() {
        String filePath = "path/to/testdata.csv"; // Replace with the precise path
        return TestDataLoader.loadTestData(filePath);
    }
}

With this strategy, there is no such thing as a have to hard-code take a look at knowledge inside the code or scripts. Take a look at knowledge may be simply up to date within the CSV file at any time when any modifications happen.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

PHP Code Snippets Powered By : XYZScripts.com