New version works well for Instrumented Unit Test for Android 19 and below.
Thanks mattmook and aaalaniz for contribution.
ParametersProvider#initialize
method was enhanced with new parameter - FrameworkMethod object. It gives access to all information connected with method in testing e.g. annotations.
public static class MethodNameReader implements ParametersProvider<CustomParameters> {
private FrameworkMethod frameworkMethod;
@Override
public void initialize(CustomParameters parametersAnnotation, FrameworkMethod frameworkMethod) {
this.frameworkMethod = frameworkMethod;
}
@Override
public Object[] getParameters() {
return new Object[]{frameworkMethod.getName()};
}
}
How to migrate? Add new parameter to ParametersProvider#initialize
method if you are using any custom parameters provider.
Thanks bohrqiu for contribution.
In some cases we can don't want to link parameters to test case by method name or putting everything into the test case annotation. We introduce possibility to define custom name for the connection by using @NamedParameters
annotation.
@Test
@Parameters(named = "return 1")
public void testSingleNamedMethod(int number) {
assertThat(number).isEqualTo(1);
}
@NamedParameters("return 1")
private Integer[] returnNumberOne() {
return new Integer[] { 1 };
}
Thanks tobyt42 for idea and contribution.
New version is noticeably faster for big as well as small set of parameters.
Thanks for ukcrpb6 for contribution.
Due to problems with test results presentation e.g. in Jenkins where test cases will be not ordered by execution but by naming, default test case naming template was changed to:
{method}({params}) [{index}]
from
[{index}] {params} ({method})
Thanks to ealgell for contribution.
Now you can apply multiple your own annotations for parameter conversion.
@Test
@Parameters("2")
public void useMultipleConvertersFromLeftToRight(@TimesTwo @PlusTwo Integer param) {
assertThat(param).isEqualTo(2 * 2 + 2);
}
Thanks to bbobcik for inspiration.
As matthopkins suggested it was a problem that tests were ignored when could not resolve provided parameters. Now tests will fail in such a situation.
java.lang.IllegalStateException: Method package.className#methodName is annotated with @Parameters but there were no parameters provided.
Thanks to the rest of contributors for lots of improvements:
Utility method $
was deprecated. It was causing too much problems and we decided not to support it any more. If you wish to keep using it, implement it in your own codebase.
@Test
@Parameters({"java.lang.Object", "java.lang.String"})
public void passClassAsString(Class<?> clazz) {
assertThat(clazz).isIn(java.lang.Object.class, java.lang.String.class);
}
Thanks to adammichalik for contribution
You can create your own annotations for parameter conversion. Just annotate it with @Param
and pass it a reference to Converter
implementation.
Example:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@Param(converter = FormattedDateConverter.class)
public @interface DateParam {
String format() default "dd.MM.yyyy";
}
public static class FormattedDateConverter implements Converter<DateParam, Date> {
private String format;
@Override
public void initialize(DateParam annotation) {
this.format = annotation.format();
}
@Override
public Date convert(Object param) throws ConversionFailedException {
try {
return new SimpleDateFormat(format).parse(param.toString());
} catch (ParseException e) {
throw new ConversionFailedException("failed");
}
}
}
Usage example:
@Test
@Parameters({"2012-12-01"})
public void testWithConvertedDate(@DateParam Date date) {
assertThat(...);
}
Thanks to bbobcik for inspiration
You can create custom annotations for parameter providers. @FileParameters
have been refactored to use this mechanism and should serve as a perfect usage example.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@CustomParameters(provider = FileParametersProvider.class)
public @interface FileParameters {
String fileLocation();
}
public class FileParametersProvider implements ParametersProvider<FileParameters> {
private String fileLocation;
@Override
public void initialize(FileParameters fileParameters) {
this.fileLocation = fileParameters.fileLocation();
}
@Override
public Object[] getParameters() {
return paramsFromFile(fileLocation);
}
...
}
Thanks to piekarskim The issue #1 is fixed. Using this annotation will result in creating a n-fold cartesian product of parameter values effectively testing each possible combination. Since an example is worth a thousand words:
Such annotated test method:
@Test
@CombinedParameters({"a,b", "1,2"})
public void calledWithCartesianProduct(String character, Integer number) {
...
}
Will be called 4 times with parameters:
a 1
a 2
b 1
b 2
Thanks to the rest of contributors for lots of bug fixes and improvements:
New annotation @TestCaseName
that can be used for test case name configuration:
@Test
@Parameters({ "1,1", "2,2" })
@TestCaseName("factorial({0}) = {1}")
public void custom_names_for_test_case(int argument, int result) { }
will produce tests with names:
factorial(1) = 1
factorial(2) = 2
Thanks to Menliat for contribution.
Parameters annotation now allows passing Enum values as parameters
@Parameters(source = Fruit.class)
Thanks to ukcrpb6 for contribution.
When starting a single test method from within an IDE, the tests results were not shown up properly in the results tab. Its fixed now thanks to jerzykrlk
Thanks to the rest of contributors for lots of bug fixes and improvements: