本文整理汇总了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);
}