I want to write a JUnit with Java for a generic method. I know that erasure happens in Java for generics but I suspect it should be possible to since there are ParametraizedTypes
.
I want a way to get a string like this: T extends Comparable<? super T>
or some way to check that method written in this format. I only want to check constraints. How can I test it in the best way?
@SafeVarargs
public static <T extends Comparable<? super T>> T findMax(T... list) {
if (list.length == 0)
return null;
T max = list[0];
for (T item : list)
{
if (item.compareTo(max) > 0)
max = item;
}
return max;
}
I want to write a test for this:
class A implements Comparable<Integer> {
@Override
public int compareTo(Integer o) {
// do something
}
}
// Test Method with JUnit
A a1 = new A();
A a2 = new A();
findMax(a1, a2); // Compile error