I need to load datasets for bunch of classes in tests.
Those classes pre generated and in theory can be changed at any moment. For now all datasets - json representation of class with filled required fields.
I use generics for that. Mapper example:
public class ResourceMapper<T> {
private Class<T> classType;
public ResourceMapper(Class<T> classType) {
this.classType = classType;
}
private T build(String path) {
InputStream inputStream = this.getClass().getResourceAsStream(path);
try {
ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());
T requiredClass = objectMapper.readValue(inputStream, this.classType);
inputStream.close();
return requiredClass ;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
Usage example:
ResourceMapper<SomeClass> resourceMapper = new ResourceMapper<>(SomeClass.class);
SomeClass someClass = resourceMapper.build("/someClass.json");
Is my solution good enough?
I have feeling, that is pretty silly.
Aucun commentaire:
Enregistrer un commentaire