@ArgumentSource annotation is used to specify custom and reusable ArgumentsProvider.
Step 1: Define ‘EmployeeArgumentsProvider’ that feeds data to test method.
public class EmployeeArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return Stream.of(
Arguments.of(1, "Krishna", "Gurram"),
Arguments.of(2, "Gopi", "Battu"),
Arguments.of(3, "Ram", "Ponnam")
);
}
}
Step 2: Use the provider EmployeeArgumentsProvider as ArgumentSource.
@ParameterizedTest
@ArgumentsSource(EmployeeArgumentsProvider.class)
void getEmployeeAsStringTest2(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();
}
}
EmployeeArgumentsProvider.java
package com.sample.app.providers;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
public class EmployeeArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return Stream.of(
Arguments.of(1, "Krishna", "Gurram"),
Arguments.of(2, "Gopi", "Battu"),
Arguments.of(3, "Ram", "Ponnam")
);
}
}
ArgumentsSourceTest.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.ArgumentsSource;
import com.sample.app.providers.EmployeeArgumentsProvider;
import com.sample.app.util.EmployeeUtil;
public class ArgumentsSourceTest {
@ParameterizedTest
@ArgumentsSource(EmployeeArgumentsProvider.class)
void getEmployeeAsStringTest(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 observe that the test method ‘getEmployeeAsStringTest’ called with the
input provided by arguments provider.
Previous Next Home
No comments:
Post a Comment