I am creating a library of sorting algorithms. Each class has the same method:
public static <T extends Comparable<? super T>> void sort(T[] array)
I would have the classes all implement an interface or extend an abstract class but the sort
method is static
which means it can't be overridden.
This is my first time using JUnit 5. I would like to run the same JUnit tests on all of the sort classes. After some research, it seems the way to do this in JUnit 5 is using @ParameterizedTest
. The way I was considering is to get the class as a parameter in the test method, get the sort
method using reflection, and invoke it. The issue is, I seem to be having an issue passing the classes to the tests. When I use @ParameterizedTest
with either @ValueSource
or @MethodSource
, the Class
in the parameter is of type java.lang.Class
.
Just to test the use of @ParameterizedTest
, I have tried the following:
@ParameterizedTest
@MethodSource("sortClasses")
public void simpleParameterizedTest(Class<?> sortClass) {
System.out.println(sortClass.getClass().getName());
}
private static Class<?>[] sortClasses() {
return new Class<?>[] {
BubbleSort.class
};
}
and
@ParameterizedTest
@ValueSource(classes = {BubbleSort.class})
public void simpleParameterizedTest(Class<?> sortClass) {
System.out.println(sortClass.getClass().getName());
}
Both print: java.lang.Class
instead of my.package.BubbleSort
.
I haven't tried it yet but I know I can get the classes using reflection with the String
names of the classes but that seems like wild overkill.
What am I missing? Is this the best approach for this in JUnit 5?
Aucun commentaire:
Enregistrer un commentaire