Like the title says whenever I run my integration tests with the Jersey Test Framework they pass when I do one test at a time. When I do a clean and build on my project it passes some tests but fails on two or three.
A message body reader for Java class com.foo.Result, and Java type class com.foo.Result, and MIME media type application/octet-stream was not found
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.RenderedImageProvider */* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
My web service resource has a @Produces and @Consumes annotation on the methods and do not understand why it some pass and others fail when the code is basically the same.
A snippet of the web resource:
@Path("/data")
@RolesAllowed({"upload"})
@Component
@Scope("request")
public final class UploadData {
@POST
@Consumes("application/vnd.foo.data-v1.0.0+xml")
@Produces("application/xml")
public Response uploadData(final Data data) {
// Processes the data and returns the response
return getResponse(data, null);
}
}
All my integration tests have something like this:
ClientResponse response = resource().path("123_10").entity(getData(), "application/vnd.foo.data-v1.0.0+xml").post(ClientResponse.class);
Result result = response.getEntity(Result.class); // Seems to fail on this line for some tests
Here is the main test class that my other test classes inherit from:
public abstract class AbstractRestTest {
private static final String PACKAGE = UploadData.class.getPackage().getName();
private static JerseyTest jerseyTest;
private Data data;
/**
* Initializes the Jersey Test to use for testing.
*/
@BeforeSuite
public final void init() {
jerseyTest = new JerseyTest(new WebAppDescriptor.Builder(PACKAGE)
.contextPath("rest").contextParam("contextConfigLocation",
"classpath:testApplicationContext.xml")
.servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.requestListenerClass(RequestContextListener.class).build()) {
};
}
/**
* Gets the Jersey Test client.
*
* @return - the Jersey Test client.
*/
public final Client client() {
return jerseyTest.client();
}
/**
* Gets the Jersey Test web resource.
*
* @return - the Jersey Test web resource.
*/
public final WebResource resource() {
return jerseyTest.resource().path("data");
}
/**
* Sets up the necessary code to run the tests.
*
* @throws Exception if it cannot set up the test.
*/
@BeforeTest
public final void setUp() throws Exception {
jerseyTest.setUp();
data = new Data();
}
/**
* Tears down the resources after the tests.
*
* @throws Exception if it cannot tear down the test.
*/
@AfterTest
public final void tearDown() throws Exception {
jerseyTest.tearDown();
}
/**
* Get the data.
*
* @return - the data.
*/
public final Data getData() {
return data;
}
/**
* Set the data.
*
* @param theData - the data.
*/
public final void setData(final Data theData) {
this.data = theData;
}
Solved it! Cannot have any code in the setup method in the AbstractRestTest. Once I removed it and only called the JerseyTest.setup() all my unit tests passed.