question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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,

  1. 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.
  2. 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:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
panchenkocommented, Nov 16, 2018

@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.

0reactions
dileepguntamadugucommented, Nov 17, 2018

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When using JUnit's @Parameterized, can I have some tests ...
However sometimes I would like to have some of the test-methods still run only once as they do not make use of the...
Read more >
A More Practical Guide to JUnit 5 Parameterized Tests
Learn how to write JUnit 5 parameterized tests. Learn answers to some of the most asked questions about parameterized tests.
Read more >
JUnit 5 Tutorial: Writing Parameterized Tests - Petri Kainulainen
This blog post describes how we can write parameterized tests with JUnit 5. After we have finished this blog post, we: Can get...
Read more >
JUnit 5 User Guide
Test classes, test methods, and lifecycle methods are not required to be public , but they must not be private . It is...
Read more >
Similar tests should be grouped in a single Parameterized test
Parameterized tests exist in most test frameworks (JUnit, TestNG, etc…​). The right balance needs of course to be found. There is no point...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found