So I am trying to invoke the JUnit Parameterized
test runner on a load of individual generic typed objects. The specific classes to be used are known to descendant classes.
Originally I managed to get a method which returns a Collection<T>
for JUnit to work with, but then I realised JUnit actually requires a Collection<T[]>
- I'm having trouble creating this array because of Java's reluctance/refusal to create arrays of generics.
protected static <T> Collection<T> someMethod(Class<T> someClass)
{
Collection<T> result = new ArrayList<T>();
T someBean;
while (someCondition(...))
{
//Do some stuff to compute someBean
result.add(someBean);
}
return result;
}
Now JUnit requires an Collection<T[]>
like this:
// (descendant class)
@Parameters
public static Collection<SomeActualClass[]> data()
{
return someMethod(SomeActualClass.class);
}
So I want to change the return type by doing something like:
protected static <T> Collection<T[]> someMethod(Class<T> someClass)
{
Collection<T[]> result = new ArrayList<T>();
T someBean;
while (someCondition(...))
{
//Do some stuff to compute someBean
result.add(new T[]{someBean});
}
return result;
}
Unfortunately Java won't let me do this because you can't create generic arrays.
I'm tempted to put the statement as result.add((T[])new Object[]{someBean});
but is this safe? or is there a better way?