Junit Parameterization without private properties
See original GitHub issue**This is more of a query than an issue and I’m very Naive to Junit
I’m working on a Test Automation project which is based on Junit + Selenium. I would be needing a lot of test data for each of the scenario.
I’ve maintained a csv file to store the data, where in I’m reading the csv data and then mapping the header columns to the records values as a list of object.
Is it possible for Junit Parameterized tests running without the private properties? because,
- There would be so many fields in the csv file and it would be a tedious job to create private properties for each of the test data.
- The getRecords() method will return the data only as a object row mappings and it is not appropriate to define some private property for this row data.
`package core.utilities;
import org.junit.Before; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord;
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.LinkedHashMap; import java.util.List;
@RunWith(Parameterized.class) public class JunitParameterizedRunner {
@Parameters
public static Collection getRecords(final String filename) throws IOException {
Reader in = new InputStreamReader(new FileInputStream(new File(filename)));
CSVParser csvParser = CSVParser.parse(in,CSVFormat.RFC4180.withFirstRecordAsHeader());
Map<String, Integer> csvHeaderMapRaw = csvParser.getHeaderMap();
HashMap<Integer, String> csvHeaderMapRawTailored = new HashMap<>();
int counter = 0;
for (String key : csvHeaderMapRaw.keySet()) {
System.out.println(key);
csvHeaderMapRawTailored.put(counter++,key);
}
Iterable<CSVRecord> records = csvParser.getRecords();
LinkedHashMap<String, String> recordDataMap = new LinkedHashMap<>();
LinkedHashMap<Long, Object> finalRecordDataMap = new LinkedHashMap<>();
for (CSVRecord record : records) {
for(int valueIndex=0;valueIndex<csvHeaderMapRaw.size();valueIndex++){
recordDataMap.put(csvHeaderMapRawTailored.get(valueIndex), record.get(valueIndex));
}
finalRecordDataMap.put(record.getRecordNumber(), recordDataMap.clone());
}
List<Object> list = new ArrayList<Object>(finalRecordDataMap.values());
System.out.println("The Records DataMap is: "+Arrays.asList(list));
return Arrays.asList(list);
}
@Test
public void dataStream() {
try {
String fileName = "C:\\testData.csv";
getRecords(fileName).toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}`
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
@dileepguntamadugu To be honest, it’s not clear if your question is about using JUnit (which does not care about “private properties”) or designing classes for the test data.
@Tibor17 Thank you. As i said earlier junit is new to me and didn’t ever try the dataprovider, i’ll give it a try and analyze as to how it will work on my project.