Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to display a name/label for each Parameterized Junit test in Eclipse [closed]
- public class PolySuite extends Suite {
- // //////////////////////////////
- // Public helper interfaces
- /**
- * Annotation for a method which returns a {@link Configuration}
- * to be injected into the test class constructor
- */
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public static @interface Config {
- }
- public static interface Configuration {
- int size();
- Object getTestValue(int index);
- String getTestName(int index);
- }
- // //////////////////////////////
- // Fields
- private final List<Runner> runners;
- // //////////////////////////////
- // Constructor
- /**
- * Only called reflectively. Do not use programmatically.
- * @param c the test class
- * @throws Throwable if something bad happens
- */
- public PolySuite(Class<?> c) throws Throwable {
- super(c, Collections.<Runner>emptyList());
- TestClass testClass = getTestClass();
- Class<?> jTestClass = testClass.getJavaClass();
- Configuration configuration = getConfiguration(testClass);
- List<Runner> runners = new ArrayList<Runner>();
- for (int i = 0, size = configuration.size(); i < size; i++) {
- SingleRunner runner = new SingleRunner(jTestClass, configuration.getTestValue(i), configuration.getTestName(i));
- runners.add(runner);
- }
- this.runners = runners;
- }
- // //////////////////////////////
- // Overrides
- @Override
- protected List<Runner> getChildren() {
- return runners;
- }
- // //////////////////////////////
- // Private
- private Configuration getConfiguration(TestClass testClass) throws Throwable {
- return (Configuration) getConfigMethod(testClass).invokeExplosively(null);
- }
- private FrameworkMethod getConfigMethod(TestClass testClass) {
- List<FrameworkMethod> methods = testClass.getAnnotatedMethods(Config.class);
- if (methods.isEmpty()) {
- throw new IllegalStateException("@" + Config.class.getSimpleName() + " method not found");
- }
- if (methods.size() > 1) {
- throw new IllegalStateException("Too many @" + Config.class.getSimpleName() + " methods");
- }
- FrameworkMethod method = methods.get(0);
- int modifiers = method.getMethod().getModifiers();
- if (!(Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
- throw new IllegalStateException("@" + Config.class.getSimpleName() + " method "" + method.getName() + "" must be public static");
- }
- return method;
- }
- // //////////////////////////////
- // Helper classes
- private static class SingleRunner extends BlockJUnit4ClassRunner {
- private final Object testVal;
- private final String testName;
- SingleRunner(Class<?> testClass, Object testVal, String testName) throws InitializationError {
- super(testClass);
- this.testVal = testVal;
- this.testName = testName;
- }
- @Override
- protected Object createTest() throws Exception {
- return getTestClass().getOnlyConstructor().newInstance(testVal);
- }
- @Override
- protected String getName() {
- return testName;
- }
- @Override
- protected String testName(FrameworkMethod method) {
- return testName + ": " + method.getName();
- }
- @Override
- protected void validateConstructor(List<Throwable> errors) {
- validateOnlyOneConstructor(errors);
- }
- @Override
- protected Statement classBlock(RunNotifier notifier) {
- return childrenInvoker(notifier);
- }
- }
- }
- @RunWith(PolySuite.class)
- public class PolySuiteExample {
- // //////////////////////////////
- // Fixture
- @Config
- public static Configuration getConfig() {
- return new Configuration() {
- @Override
- public int size() {
- return 10;
- }
- @Override
- public Integer getTestValue(int index) {
- return index * 2;
- }
- @Override
- public String getTestName(int index) {
- return "test" + index;
- }
- };
- }
- // //////////////////////////////
- // Fields
- private final int testVal;
- // //////////////////////////////
- // Constructor
- public PolySuiteExample(int testVal) {
- this.testVal = testVal;
- }
- // //////////////////////////////
- // Test
- @Ignore
- @Test
- public void odd() {
- assertFalse(testVal % 2 == 0);
- }
- @Test
- public void even() {
- assertTrue(testVal % 2 == 0);
- }
- }
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- 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.Arrays;
- 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;
- public class LabelledParameterized extends CompositeRunner {
- static class TestClassRunnerForParameters extends JUnit4ClassRunner {
- private final Object[] fParameters;
- private final String fParameterFirstValue;
- private final Constructor<?> fConstructor;
- TestClassRunnerForParameters(TestClass testClass, Object[] parameters, int i) throws InitializationError {
- super(testClass.getJavaClass()); // todo
- fParameters = parameters;
- if (parameters != null) {
- fParameterFirstValue = Arrays.asList(parameters).toString();
- } else {
- fParameterFirstValue = String.valueOf(i);
- }
- fConstructor = getOnlyConstructor();
- }
- @Override
- protected Object createTest() throws Exception {
- return fConstructor.newInstance(fParameters);
- }
- @Override
- protected String getName() {
- return String.format("%s", fParameterFirstValue);
- }
- @Override
- protected String testName(final Method method) {
- return String.format("%s%s", method.getName(), fParameterFirstValue);
- }
- 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);
- }
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public static @interface Parameters {
- }
- private final TestClass fTestClass;
- public LabelledParameterized(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<Method> 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<Object[]> eachOne(Object... params) {
- List<Object[]> results = new ArrayList<Object[]>();
- for (Object param : params)
- results.add(new Object[] { param });
- return results;
- }
- }
- @Parameters(name="namestring")
- @RunWith(Parameterized.class)
- static public class FibonacciTest {
- @Parameters(name= "{index}: fib({0})={1}")
- public static Iterable<Object[]> data() {
- return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
- { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
- }
- private final int fInput;
- private final int fExpected;
- public FibonacciTest(int input, int expected) {
- fInput= input;
- fExpected= expected;
- }
- @Test
- public void test() {
- assertEquals(fExpected, fib(fInput));
- }
- private int fib(int x) {
- return 0;
- }
- }
- @Test
- public void name() {
- Assert.assertEquals("", inboundFileName);
- }
- private <T> void assertThat(final T actual, final Matcher<T> expected) {
- Assert.assertThat(editThisToDisplaySomethingForYourDatum, actual, expected);
- }
- public ExampleTest(final String testLabel, final int one, final int two) {
- this.testLabel = testLabel;
- // ...
- }
- @Parameters
- public static Collection<Object[]> data() {
- return asList(new Object[][]{
- {"first test", 3, 4},
- {"second test", 5, 6}
- });
- }
Add Comment
Please, Sign In to add comment