Wednesday 21 July 2021

Junit5: ParameterizedTest: CsvFileSource: Populate method parameters from a csv file

@CsvFileSource annotation is used to populate method parameters from csv files from the class path.

 

For example, I created ‘employeeInfo.csv’ file under src/test/resources folder.

 

employeeInfo.csv

1, Krishna, Gurram
2, Gopi, Battu
3, Ram, Ponnam

 

We can pass the data from employee.csv as arguments to test method using @CsvFileSource annotation.

@ParameterizedTest
@CsvFileSource(resources = "/employeeInfo.csv")
void testWithCsvSource_1(int id, String firstName, String lastName) {
	Employee emp = new Employee(id, firstName, lastName);
	
	String expected = emp.getId()+","+emp.getFirstName()+","+emp.getLastName();
	String actual = EmployeeUtil.getEmployeeAsString(emp);
	
	assertEquals(expected, actual);
}

 

How to handle if csv file has header?

Using 'numLinesToSkip' attribte, you can skip number of lines. Since header span single line, I can skip one line and proceed with data.

 

employeeInfoWithHeaders.csv

id, firstName, lastName
1, Krishna, Gurram
2, Gopi, Battu
3, Ram, Ponnam

 

Using 'numLinesToSkip' attribte, you can skip number of lines.

@ParameterizedTest
@CsvFileSource(resources = "/employeeInfoWithHeaders.csv", numLinesToSkip = 1)
void testWithCsvSource_2(int id, String firstName, String lastName) {
	Employee emp = new Employee(id, firstName, lastName);
	
	String expected = emp.getId()+","+emp.getFirstName()+","+emp.getLastName();
	String actual = EmployeeUtil.getEmployeeAsString(emp);
	
	assertEquals(expected, actual);
}

 

Find the below working application.

 

Employee.java

 

package com.sample.app;

public class Employee {

	private int id;
	private String firstName;
	private String lastName;

	public Employee() {

	}

	public Employee(int id, String firstName, String lastName) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

}

 

EmployeeUtil.java

package com.sample.app.util;

import com.sample.app.Employee;

public class EmployeeUtil {

	public static String getEmployeeAsString(Employee emp) {
		if(emp == null) {
			return "none";
		}
		
		return emp.getId()+","+emp.getFirstName()+","+emp.getLastName();
	}
}

 

CsvFileDemo.java

package com.sample.app;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

import com.sample.app.util.EmployeeUtil;

public class CsvFileDemo {
	@ParameterizedTest
	@CsvFileSource(resources = "/employeeInfo.csv")
	void testWithCsvSource_1(int id, String firstName, String lastName) {
		Employee emp = new Employee(id, firstName, lastName);
		
		String expected = emp.getId()+","+emp.getFirstName()+","+emp.getLastName();
		String actual = EmployeeUtil.getEmployeeAsString(emp);
		
		assertEquals(expected, actual);
	}
	
	@ParameterizedTest
	@CsvFileSource(resources = "/employeeInfoWithHeaders.csv", numLinesToSkip = 1)
	void testWithCsvSource_2(int id, String firstName, String lastName) {
		Employee emp = new Employee(id, firstName, lastName);
		
		String expected = emp.getId()+","+emp.getFirstName()+","+emp.getLastName();
		String actual = EmployeeUtil.getEmployeeAsString(emp);
		
		assertEquals(expected, actual);
	}
}

 

Run above application and you can observe that the test cases executed with the data from csv files.

 


 

   

Previous                                                    Next                                                    Home

No comments:

Post a Comment