Wednesday, 4 August 2021

Junit5: ParameterizedTest: Custom Aggregators

Follow below procedure to implement custom aggregators.

 

Step 1: Implement ArgumentsAggregator interface.

public class PersonAggregator implements ArgumentsAggregator {

	@Override
	public Person aggregateArguments(ArgumentsAccessor arguments, ParameterContext context)
			throws ArgumentsAggregationException {
		return new Person(arguments.getString(0), arguments.getString(1), arguments.get(2, Gender.class),
				arguments.get(3, LocalDate.class));
	}

}

Step 2: Use the aggregator defined in step 1.

void testWithArgumentsAggregator(@AggregateWith(PersonAggregator.class) Person person) {
	if (person.getFirstName().equals("Sailu")) {
		assertEquals(Gender.FEMALE, person.getGender());
		assertEquals("PTR", person.getLastName());
		assertEquals(1989, person.getDateOfBirth().getYear());
	} else {
		assertEquals(Gender.MALE, person.getGender());
		assertEquals("Ram", person.getFirstName());
		assertEquals("Gurram", person.getLastName());
		assertEquals(1990, person.getDateOfBirth().getYear());
	}
}


Find the below working application.

 

Gender.java

package com.sample.app.model;

public enum Gender {
	FEMALE, MALE

}


Person.java

package com.sample.app.model;

import java.time.LocalDate;

public class Person {
	private String firstName;
	private String lastName;
	private Gender gender;
	private LocalDate dateOfBirth;

	public Person(String firstName, String lastName, Gender gender, LocalDate dateOfBirth) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.gender = gender;
		this.dateOfBirth = dateOfBirth;
	}

	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;
	}

	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public LocalDate getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(LocalDate dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

}


PersonAggregator.java

package com.sample.app.aggregators;

import java.time.LocalDate;

import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.aggregator.ArgumentsAggregationException;
import org.junit.jupiter.params.aggregator.ArgumentsAggregator;

import com.sample.app.model.Gender;
import com.sample.app.model.Person;

public class PersonAggregator implements ArgumentsAggregator {

	@Override
	public Person aggregateArguments(ArgumentsAccessor arguments, ParameterContext context)
			throws ArgumentsAggregationException {
		return new Person(arguments.getString(0), arguments.getString(1), arguments.get(2, Gender.class),
				arguments.get(3, LocalDate.class));
	}

}


CustomAggregatorTest.java

package com.sample.app;

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

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.AggregateWith;
import org.junit.jupiter.params.provider.CsvSource;

import com.sample.app.aggregators.PersonAggregator;
import com.sample.app.model.Gender;
import com.sample.app.model.Person;

public class CustomAggregatorTest {
	
	@ParameterizedTest
	@CsvSource({ 
				"Sailu, PTR, FEMALE, 1989-08-20", 
				"Ram, Gurram, MALE, 1990-01-22"
			})
	void testWithArgumentsAggregator(@AggregateWith(PersonAggregator.class) Person person) {
		if (person.getFirstName().equals("Sailu")) {
			assertEquals(Gender.FEMALE, person.getGender());
			assertEquals("PTR", person.getLastName());
			assertEquals(1989, person.getDateOfBirth().getYear());
		} else {
			assertEquals(Gender.MALE, person.getGender());
			assertEquals("Ram", person.getFirstName());
			assertEquals("Gurram", person.getLastName());
			assertEquals(1990, person.getDateOfBirth().getYear());
		}
	}
}


Run above test class, you will see the test result in junit window.





You can even create a custom annotation that is composed of multiple annotations use.


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AggregateWith(PersonAggregator.class)
public @interface CsvToPerson {
	
}


@ParameterizedTest
@CsvSource({ 
			"Sailu, PTR, FEMALE, 1989-08-20", 
			"Ram, Gurram, MALE, 1990-01-22"
		})
void testWithArgumentsAggregator(@CsvToPerson Person person) {
	.......
	.......
}


Find the below working application.

 

CsvToPerson.java

package com.sample.app.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.params.aggregator.AggregateWith;

import com.sample.app.aggregators.PersonAggregator;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AggregateWith(PersonAggregator.class)
public @interface CsvToPerson {
	
}


CustomAggregatorTest2.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.annotations.CsvToPerson;
import com.sample.app.model.Gender;
import com.sample.app.model.Person;

public class CustomAggregatorTest2 {
	@ParameterizedTest
	@CsvSource({ 
				"Sailu, PTR, FEMALE, 1989-08-20", 
				"Ram, Gurram, MALE, 1990-01-22"
			})
	void testWithArgumentsAggregator(@CsvToPerson Person person) {
		if (person.getFirstName().equals("Sailu")) {
			assertEquals(Gender.FEMALE, person.getGender());
			assertEquals("PTR", person.getLastName());
			assertEquals(1989, person.getDateOfBirth().getYear());
		} else {
			assertEquals(Gender.MALE, person.getGender());
			assertEquals("Ram", person.getFirstName());
			assertEquals("Gurram", person.getLastName());
			assertEquals(1990, person.getDateOfBirth().getYear());
		}
	}
}

 

 

 





 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment