就像标题所说,每当我使用Jersey测试框架运行集成测试时,他们会在我一次进行一次测试时通过。当我进行清理并在我的项目上构建时,它会通过一些测试但在两到三个时失败。
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
我的Web服务资源在方法上有一个@Produces和@Consumes注释,并且当代码基本相同时,不理解它为什么会通过而其他人会失败。
网络资源的摘录:
@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);
}
}
我的所有集成测试都有类似的内容:
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
这是我的其他测试类继承的主要测试类:
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;
}
答案 0 :(得分:0)
解决了! AbstractRestTest中的setup方法中没有任何代码。一旦我删除了它并且只调用了JerseyTest.setup(),我的所有单元测试都通过了。
/**
* 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(); // Removed and set it in the tests I needed it to be in, this was the problem having this line before or after the jerseyTest.setup()
}