Created
August 11, 2011 23:13
-
-
Save chadnickbok/1141041 to your computer and use it in GitHub Desktop.
NamedParameterized Test Runner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.junit.runners; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.junit.runners.Parameterized.TestClassRunnerForParameters; | |
import org.junit.runners.model.FrameworkMethod; | |
import org.junit.runners.model.InitializationError; | |
import org.junit.runners.model.TestClass; | |
/** | |
* <p> | |
* The custom runner <code>NamedParameterized</code> implements named | |
* parameterixed tests. When running a named-parameterized test class, instances | |
* are created for the cross-product of the test methods and the test data | |
* elements. | |
* | |
* In addition, each test is named based on the final parameter given in the | |
* object list, and this parameter is not passed to the test-class constructor. | |
* </p> | |
* | |
*/ | |
public class NamedParameterized extends Parameterized { | |
protected class TestClassRunnerForNamedParameters extends | |
TestClassRunnerForParameters { | |
TestClassRunnerForNamedParameters(Class<?> type, | |
List<Object[]> parameterList, int i) throws InitializationError { | |
super(type, parameterList, i); | |
} | |
@Override | |
protected String getName() { | |
StringBuffer name= new StringBuffer(); | |
Object[] params = fParameterList.get(fParameterSetNumber); | |
name.append(String.format("_[%s]", fParameterSetNumber)); | |
for (Object param : params) { | |
String paramString; | |
if (param.getClass() == String.class) { | |
paramString = (String) param; | |
} else if (param instanceof Integer) { | |
paramString = Integer.toString((Integer)param); | |
} else { | |
paramString = param.toString(); | |
} | |
name.append("_" + paramString); | |
} | |
return name.toString(); | |
} | |
@Override | |
protected String testName(final FrameworkMethod method) { | |
return String.format("%s%s", method.getName(), getName()); | |
} | |
} | |
/** | |
* Only called reflectively. Do not use programmatically. | |
*/ | |
public NamedParameterized(Class<?> klass) throws Throwable { | |
super(klass); | |
} | |
@Override | |
protected void addTestClassRunnerForParameters ( | |
TestClass testClass, List<Object[]> parametersList, int i) | |
throws Throwable { | |
runners.add(new TestClassRunnerForNamedParameters( | |
getTestClass().getJavaClass(), parametersList, i)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment