I created my resources to handle some images, and I wanted to test them with JUnit's @ClassRule
, as I did before. They look like this :
@Path("/myImage")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postImage(
@FormDataParam("file") InputStream inputStream) {
//doStuff
}
Now, I wanted to test it, and I have a problem with it. I fought that this class rule will be ok
@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
.addResource(new MyResource())
.addResource(new MultiPartBundle())
.build();
But I still get an Error
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public ...
How to write a proper class rule for this issue?
The error is because you have not register the MutliPartFeature
with the server. The MultiPartBundle
(which registers the MultiPartFeature
) is not something that is supported with the ResourceTestRule
. So you just need to register it yourself
public static final ResourceTestRule resources = ResourceTestRule.builder()
.addResource(new MyResource())
.addProvider(MultiPartFeature.class)
.build();
Same with the client. You will also need to register the feature if you want to use multipart the serialize on the client side
resource.client().register(MultiPartFeature.class)..
You can see a full example here