You possibly can create a CSV file and put your take a look at information 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 information. I’m utilizing the next for instance:
Identify,Emp Id,Division
abc,123,xyz
You possibly can modify the information in your CSV file based on your take a look at necessities.
Use the next code as a reference:
public class YourTestClass {
@ParameterizedTest
@CsvFileSource(assets = "/your_data_file.csv", numLinesToSkip = 1)
// Assuming your CSV file is within the assets listing and the primary line incorporates headers
public void testMethod(String title, int empId, String division) {
System.out.println("Identify: " + title + ", Emp Id: " + empId + ", Division: " + division);
// Add your assertions or take a look at logic right here
// For instance:
assertEquals("xyz", division);
}
}
Alternatively, when you’ve got a big record of take a look at information, you should utilize a CSV parsing library to learn the information from the file and retailer it in a map. Beneath 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 incorporates 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("Check" + index++, rowData);
}
} catch (IOException e) {
e.printStackTrace();
// Deal with the exception based on your necessities
}
return testDataMap;
}
}
Use the map in your JUnit take a look at class:
import org.junit.Check;
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 title;
non-public int empId;
non-public String division;
// Constructor to obtain the parameters
public YourTestClass(String title, int empId, String division) {
this.title = title;
this.empId = empId;
this.division = division;
}
@Check
public void testMethod() {
// Your take a look at logic utilizing the parameters
System.out.println("Identify: " + title + ", 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 information() {
String filePath = "path/to/testdata.csv"; // Replace with the precise path
return TestDataLoader.loadTestData(filePath);
}
}
With this strategy, there isn’t a have to hard-code take a look at information inside the code or scripts. Check information may be simply up to date within the CSV file at any time when any modifications happen.
