本文整理匯總了Java中org.junit.runners.Parameterized.Parameter類的典型用法代碼示例。如果您正苦於以下問題:Java Parameter類的具體用法?Java Parameter怎麽用?Java Parameter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Parameter類屬於org.junit.runners.Parameterized包,在下文中一共展示了Parameter類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createTestUsingFieldInjection
import org.junit.runners.Parameterized.Parameter; //導入依賴的package包/類
private Object createTestUsingFieldInjection() throws Exception {
List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
if (annotatedFieldsByParameter.size() != parameters.length) {
throw new Exception(
"Wrong number of parameters and @Parameter fields."
+ " @Parameter fields counted: "
+ annotatedFieldsByParameter.size()
+ ", available parameters: " + parameters.length
+ ".");
}
Object testClassInstance = getTestClass().getJavaClass().newInstance();
for (FrameworkField each : annotatedFieldsByParameter) {
Field field = each.getField();
Parameter annotation = field.getAnnotation(Parameter.class);
int index = annotation.value();
try {
field.set(testClassInstance, parameters[index]);
} catch (IllegalArgumentException iare) {
throw new Exception(getTestClass().getName()
+ ": Trying to set " + field.getName()
+ " with the value " + parameters[index]
+ " that is not the right type ("
+ parameters[index].getClass().getSimpleName()
+ " instead of " + field.getType().getSimpleName()
+ ").", iare);
}
}
return testClassInstance;
}
示例2: validateFields
import org.junit.runners.Parameterized.Parameter; //導入依賴的package包/類
@Override
protected void validateFields(List<Throwable> errors) {
super.validateFields(errors);
if (getInjectionType() == InjectionType.FIELD) {
List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
int[] usedIndices = new int[annotatedFieldsByParameter.size()];
for (FrameworkField each : annotatedFieldsByParameter) {
int index = each.getField().getAnnotation(Parameter.class)
.value();
if (index < 0 || index > annotatedFieldsByParameter.size() - 1) {
errors.add(new Exception("Invalid @Parameter value: "
+ index + ". @Parameter fields counted: "
+ annotatedFieldsByParameter.size()
+ ". Please use an index between 0 and "
+ (annotatedFieldsByParameter.size() - 1) + "."));
} else {
usedIndices[index]++;
}
}
for (int index = 0; index < usedIndices.length; index++) {
int numberOfUse = usedIndices[index];
if (numberOfUse == 0) {
errors.add(new Exception("@Parameter(" + index
+ ") is never used."));
} else if (numberOfUse > 1) {
errors.add(new Exception("@Parameter(" + index
+ ") is used more than once (" + numberOfUse + ")."));
}
}
}
}
示例3: searchParameters
import org.junit.runners.Parameterized.Parameter; //導入依賴的package包/類
public Map<String, Object> searchParameters() {
final Map<String, Object> fields = new HashMap<String, Object>();
for (final Field field : description.getDeclaringClass().getFields()) {
if (field.isAnnotationPresent(Parameter.class)) {
try {
fields.put(field.getName(), field.get(target));
} catch (IllegalArgumentException | IllegalAccessException e) {
LOGGER.error("Could not obtain the field {} in the junit test case, marks it public",
field.getName(), e);
}
}
}
return fields;
}
示例4: createTestUsingFieldInjection
import org.junit.runners.Parameterized.Parameter; //導入依賴的package包/類
private Object createTestUsingFieldInjection() throws Exception {
List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
if (annotatedFieldsByParameter.size() != fParameters.length) {
throw new Exception(
"Wrong number of parameters and @Parameter fields."
+ " @Parameter fields counted: "
+ annotatedFieldsByParameter.size()
+ ", available parameters: " + fParameters.length
+ ".");
}
Object testClassInstance = getTestClass().getJavaClass().newInstance();
for (FrameworkField each : annotatedFieldsByParameter) {
Field field = each.getField();
Parameter annotation = field.getAnnotation(Parameter.class);
int index = annotation.value();
try {
field.set(testClassInstance, fParameters[index]);
} catch (IllegalArgumentException iare) {
throw new Exception(getTestClass().getName()
+ ": Trying to set " + field.getName()
+ " with the value " + fParameters[index]
+ " that is not the right type ("
+ fParameters[index].getClass().getSimpleName()
+ " instead of " + field.getType().getSimpleName()
+ ").", iare);
}
}
return testClassInstance;
}
示例5: validateFields
import org.junit.runners.Parameterized.Parameter; //導入依賴的package包/類
@Override
protected void validateFields(List<Throwable> errors) {
super.validateFields(errors);
if (fieldsAreAnnotated()) {
List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
int[] usedIndices = new int[annotatedFieldsByParameter.size()];
for (FrameworkField each : annotatedFieldsByParameter) {
int index = each.getField().getAnnotation(Parameter.class)
.value();
if (index < 0 || index > annotatedFieldsByParameter.size() - 1) {
errors.add(new Exception("Invalid @Parameter value: "
+ index + ". @Parameter fields counted: "
+ annotatedFieldsByParameter.size()
+ ". Please use an index between 0 and "
+ (annotatedFieldsByParameter.size() - 1) + "."));
} else {
usedIndices[index]++;
}
}
for (int index = 0; index < usedIndices.length; index++) {
int numberOfUse = usedIndices[index];
if (numberOfUse == 0) {
errors.add(new Exception("@Parameter(" + index
+ ") is never used."));
} else if (numberOfUse > 1) {
errors.add(new Exception("@Parameter(" + index
+ ") is used more than once (" + numberOfUse + ")."));
}
}
}
}
示例6: createTestUsingFieldInjection
import org.junit.runners.Parameterized.Parameter; //導入依賴的package包/類
private Object createTestUsingFieldInjection() throws Exception {
List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
if (annotatedFieldsByParameter.size() != fParameters.length) {
throw new Exception(String.format(
"Wrong number of parameters and @Parameter fields. @Parameter fields counted: %s, available parameters: %s.",
annotatedFieldsByParameter.size(), fParameters.length));
}
Object testClassInstance = getTestClass().getJavaClass().newInstance();
if (getTestClass().getJavaClass() == JExUnitBase.class) {
((JExUnitBase) testClassInstance).setTestType(testType);
}
for (FrameworkField each : annotatedFieldsByParameter) {
Field field = each.getField();
Parameter annotation = field.getAnnotation(Parameter.class);
int index = annotation.value();
try {
if (field.isAccessible()) {
field.set(testClassInstance, fParameters[index]);
} else {
field.setAccessible(true);
field.set(testClassInstance, fParameters[index]);
field.setAccessible(false);
}
} catch (IllegalArgumentException iare) {
throw new Exception(
String.format(
"%s: Trying to set %s with the value %s that is not the right type (%s instead of %s).",
getTestClass().getName(), field.getName(), fParameters[index],
fParameters[index].getClass().getSimpleName(), field.getType().getSimpleName()),
iare);
}
}
return testClassInstance;
}
示例7: validateFields
import org.junit.runners.Parameterized.Parameter; //導入依賴的package包/類
@Override
protected void validateFields(List<Throwable> errors) {
super.validateFields(errors);
if (fieldsAreAnnotated()) {
List<FrameworkField> annotatedFieldsByParameter = getAnnotatedFieldsByParameter();
int[] usedIndices = new int[annotatedFieldsByParameter.size()];
for (FrameworkField each : annotatedFieldsByParameter) {
int index = each.getField().getAnnotation(Parameter.class).value();
if (index < 0 || index > annotatedFieldsByParameter.size() - 1) {
errors.add(new Exception(String.format(
"Invalid @Parameter value: %s. @Parameter fields counted: %s. Please use an index between 0 and %s.",
index, annotatedFieldsByParameter.size(), annotatedFieldsByParameter.size() - 1)));
} else {
usedIndices[index]++;
}
}
for (int index = 0; index < usedIndices.length; index++) {
int numberOfUse = usedIndices[index];
if (numberOfUse == 0) {
errors.add(new Exception(String.format("@Parameter(%s) is never used.", index)));
} else if (numberOfUse > 1) {
errors.add(new Exception(
String.format("@Parameter(%s) is used more than once (%s).", index, numberOfUse)));
}
}
}
}
示例8: getAnnotatedFieldsByParameter
import org.junit.runners.Parameterized.Parameter; //導入依賴的package包/類
private List<FrameworkField> getAnnotatedFieldsByParameter() {
return getTestClass().getAnnotatedFields(Parameter.class);
}
示例9: getAnnotatedFieldsByParameter
import org.junit.runners.Parameterized.Parameter; //導入依賴的package包/類
private List<FrameworkField> getAnnotatedFieldsByParameter() {
return getTestClass().getAnnotatedFields(Parameter.class);
}