0

正如标题所说,每当我使用 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;
     }
4

1 回答 1

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()
}
于 2013-01-08T14:26:02.517 回答