dimanche 9 juillet 2017

Using java reflection to unit test that a specific class is used in a method implementation

I want to implement and unit test a method that aggregates some data using Java 8 Stream API (the Stream interface and Collectors class). See the code below:

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AggregationKata {

    public static Map<String, Double> getAverageGradeByDepartment(Stream<Student> students) {

        return students
                .collect(Collectors.groupingBy(Student::getDepartment,
                        Collectors.averagingDouble(Student::getGrade)));

    }
}

I want to write a unit test that will enforce using the Stream API while implementing this method. In other words, Stream interface and Collectors class must be used. I guess I need to use java reflection in my unit test, but I cannot figure out how.

The question is - how do I write such a unit test? Thanks!





Aucun commentaire:

Enregistrer un commentaire