-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
automatic paramter conversion as Class in Input Method #30
Comments
Not right now as far as I can tell, but this would be awesome. I think it could be possible using argument aggregation, and in theory you might be able to write something that did this automatically using JSON binding / Jackson / Gson. Let me know how you go with it and also feel free to submit a PR if there’s something that seems to fit this library in this idea. |
Hi, public class JsonFileArgument {
private Class cls;
private String strObject;
//Getter & Setter
} I add convertor for JSON Conversion & Annotation as public @interface JsonFileSource {
String resource();
Class clsObj();
}
public class JsonArgumentConvertor implements ArgumentConverter {
@Override
public Object convert(Object source, ParameterContext context) {
if (!(source instanceof JsonFileArgument)) {
throw new ArgumentConversionException("Not a JsonFileArgument type");
}
ObjectMapper objectMapper=new ObjectMapper();
JsonFileArgument jsonFileArgument= (JsonFileArgument)source;
try {
return objectMapper.readValue(jsonFileArgument.getStrObject(),jsonFileArgument.getCls());
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
} and Finally I Change JsonFileArgumentProvider as follow @Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception
{
List<String> lstResources=getResourceFiles(this.resourceDirectory);
return lstResources.stream()
.map(resource -> readStream(context,resource))
.map(Arguments::of);
}
private JsonFileArgument readStream(ExtensionContext context, String resource) {
try{
Class<?> testClass = context.getRequiredTestClass();
InputStream inputStream = inputStreamProvider.apply(testClass, resource);
String strFileContent= IOUtils.toString(inputStream,"UTF-8");
return new JsonFileArgument(this.clsObject,strFileContent);
}catch (Exception ex){
ex.printStackTrace();
return new JsonFileArgument(this.clsObject);
}
} let me know , what do you think about my changes. |
Hi, sorry that I missed seeing this comment until just now. Would you be interested in submitting a PR with some tests to show a bit more how this works? I really like this idea. Please avoid the Hungarian notation style (clsXXX, strXXX). I find that it often makes code more difficult to read than without. |
Closing. See #177 for more info. |
Hi,
Is it possible to use class instead of JsonObject as Method Input Parameter,
For example:
@ParameterizedTest
@JsonSource("{name:'test',family:'test'}")
@DisplayName("provides an array of objects")
void testObjectPerson(Person person) {
...
}
Thanks in advance
The text was updated successfully, but these errors were encountered: