koncept.junit.runner.Parameterized Maven / Gradle / Ivy
package koncept.junit.runner;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Assert;
import org.junit.internal.runners.ClassRoadie;
import org.junit.internal.runners.CompositeRunner;
import org.junit.internal.runners.InitializationError;
import org.junit.internal.runners.JUnit4ClassRunner;
import org.junit.internal.runners.MethodValidator;
import org.junit.internal.runners.TestClass;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Parameterized.Parameters;
/** The custom runner Parameterized implements parameterized
* tests. When running a parameterized test class, instances are created for the
* cross-product of the test methods and the test data elements.
*
* For example, to test a Fibonacci function, write:
*
* @RunWith(Parameterized.class)
* public class FibonacciTest {
* @Parameters
* public static Collection
*
* Each instance of FibonacciTest will be constructed using the two-argument
* constructor and the data values in the @Parameters method.
*/
public class Parameterized extends CompositeRunner {
static class TestClassRunnerForParameters extends JUnit4ClassRunner {
private final Object[] fParameters;
private final int fParameterSetNumber;
private final Constructor> fConstructor;
TestClassRunnerForParameters(TestClass testClass, Object[] parameters, int i) throws InitializationError {
super(testClass.getJavaClass()); //todo
fParameters= parameters;
fParameterSetNumber= i;
fConstructor= getOnlyConstructor();
}
public String parametersDetails() {
if (fParameters.length == 0) return "";
StringBuilder details = new StringBuilder();
details.append(fParameters[0]);
for(int i = 1; i < fParameters.length; i++) {
details.append(", ");
details.append(paramToString(fParameters[i]));
}
return details.toString();
}
public String paramToString(Object param) {
if (param instanceof String) return (String)param;
if (param instanceof Number) return param.toString();
return param.toString();
}
@Override
protected Object createTest() throws Exception {
return fConstructor.newInstance(fParameters);
}
@Override
protected String getName() {
return String.format("[%s]", parametersDetails());
}
@Override
protected String testName(final Method method) {
return String.format("%s[%s]", method.getName(), fParameterSetNumber);
}
private Constructor> getOnlyConstructor() {
Constructor>[] constructors= getTestClass().getJavaClass().getConstructors();
Assert.assertEquals(1, constructors.length);
return constructors[0];
}
@Override
protected void validate() throws InitializationError {
// do nothing: validated before.
}
@Override
public void run(RunNotifier notifier) {
runMethods(notifier);
}
}
private final TestClass fTestClass;
public Parameterized(Class> klass) throws Exception {
super(klass.getName());
fTestClass= new TestClass(klass);
MethodValidator methodValidator= new MethodValidator(fTestClass);
methodValidator.validateStaticMethods();
methodValidator.validateInstanceMethods();
methodValidator.assertValid();
int i= 0;
for (final Object each : getParametersList()) {
if (each instanceof Object[])
add(new TestClassRunnerForParameters(fTestClass, (Object[])each, i++));
else
throw new Exception(String.format("%s.%s() must return a Collection of arrays.", fTestClass.getName(), getParametersMethod().getName()));
}
}
@Override
public void run(final RunNotifier notifier) {
new ClassRoadie(notifier, fTestClass, getDescription(), new Runnable() {
public void run() {
runChildren(notifier);
}
}).runProtected();
}
private Collection> getParametersList() throws IllegalAccessException, InvocationTargetException, Exception {
return (Collection>) getParametersMethod().invoke(null);
}
private Method getParametersMethod() throws Exception {
List methods= fTestClass.getAnnotatedMethods(Parameters.class);
for (Method each : methods) {
int modifiers= each.getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))
return each;
}
throw new Exception("No public static parameters method on class " + getName());
}
public static Collection © 2015 - 2025 Weber Informatics LLC | Privacy Policy