Wednesday, 21 July 2021

Junit5: ParameterizedTest: csvSource: express arguments as comma separated values

@CsvSource allows you to express argument lists as comma-separated values.

@ParameterizedTest
@CsvSource({
    "1, Krishna, Gurram",
    "2, Gopi, Battu",
    "3, Ram, Ponnam"
})
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);
}

  Default delimiter is comma, you can specify the custom delimiter by specifying ‘delimiterString’ attribute. 

@ParameterizedTest
@CsvSource(value = {
    "1: Krishna: Gurram",
    "2: Gopi: Battu",
    "3: Ram: Ponnam"
}, delimiterString=":")
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);
}

  @CsvSource uses a single quote ' as its quote character ('Krishna, Govind'). 

@ParameterizedTest
@CsvSource({
    "1, 'Krishna, Govind', Gurram",
    "2, 'Gopi, krishna', Battu",
    "3, Ram, Ponnam"
})
void testWithCsvSource_3(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();
	}
}

 

CsvSourceDemo.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.CsvSource;

import com.sample.app.util.EmployeeUtil;

public class CsvSourceDemo {
	@ParameterizedTest
	@CsvSource({
	    "1, Krishna, Gurram",
	    "2, Gopi, Battu",
	    "3, Ram, Ponnam"
	})
	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
	@CsvSource(value = {
	    "1: Krishna: Gurram",
	    "2: Gopi: Battu",
	    "3: Ram: Ponnam"
	}, delimiterString=":")
	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);
	}
	
	@ParameterizedTest
	@CsvSource({
	    "1, 'Krishna, Govind', Gurram",
	    "2, 'Gopi, krishna', Battu",
	    "3, Ram, Ponnam"
	})
	void testWithCsvSource_3(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 test class, you will see the results in junit window.


 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment