org.fuwjin.util.Parameterized Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glory Show documentation
Show all versions of glory Show documentation
There's a nice knock-down argument for you
The newest version!
package org.fuwjin.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.Suite;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestClass;
/**
*
* 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 List<Object[]> data() {
* return Arrays.asList(new Object[][]{Fibonacci, {{0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8}}});
* }
*
* private int fInput;
* private int fExpected;
*
* public FibonacciTest(int input, int expected) {
* fInput = input;
* fExpected = expected;
* }
*
* @Test
* public void test() {
* assertEquals(fExpected, Fibonacci.compute(fInput));
* }
* }
*
*
* Each instance of FibonacciTest
will be constructed using the
* two-argument constructor and the data values in the
* @Parameters
method.
*
*/
public class Parameterized extends Suite {
/**
* Annotation for a method which provides parameters to be injected into the
* test class constructor by Parameterized
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Parameters {
// marker
}
private class TestClassRunnerForParameters extends BlockJUnit4ClassRunner {
private final Object[] parameters;
TestClassRunnerForParameters(final Class> type, final Object[] parameters) throws InitializationError {
super(type);
this.parameters = parameters;
}
@Override
public Object createTest() throws Exception {
return getTestClass().getOnlyConstructor().newInstance(computeParams());
}
@Override
protected Statement classBlock(final RunNotifier notifier) {
return childrenInvoker(notifier);
}
@Override
protected String getName() {
return String.format("[%s]", parameters[0]);
}
@Override
protected String testName(final FrameworkMethod method) {
return String.format("%s[%s]", method.getName(), parameters[0]);
}
@Override
protected void validateConstructor(final List errors) {
validateOnlyOneConstructor(errors);
}
private Object[] computeParams() throws Exception {
return parameters;
}
}
private final ArrayList runners = new ArrayList();
/**
* Only called reflectively. Do not call directly.
* @param klass the unit test class
* @throws Throwable if the test class cannot be parameterized
*/
public Parameterized(final Class> klass) throws Throwable {
super(klass, Collections. emptyList());
final List