tags:

views:

100

answers:

2

I ran into the following issue when setting up a JAXRS test service in a unit test. This is the code (taken from an AbstractJUnit4SpringContextTests-derived test class):

    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    sf.setServiceBeans(applicationContext.getBean("searchXY"));
    sf.setAddress("http://localhost:9000/");
    sf.create();

    restClient = new RestTestClient();//custom class for client-side testing

    ....

    InputStream dummyRequestFileAsStream = getInputStreamForClasspathResource(
    DUMMY_REQUEST_FILE);
    LOGGER.info("Testing searchQuery ReST service access");
    int httpStatus = restClient.postXmlStream(
                    "http://localhost:9000/search/searchXY",
                    dummyRequestFileAsStream);

I'm feeding an XML test file into the service. CXF would impertinently try to wrap the xml into a javax.xml.bind.JAXBElement, invoke the service, and fail with an IllegalArgumentException (in the reflection API) because the service of course does not accept a JAX-RS-specific element but rather the SearchRequest element that I defined in my XSD before.

However, when I insert the following line into my spring context, everything's fine:

<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />

Anyone else seen this?

+1  A: 

It's difficult to see why the original issue is happening without more details being provided. The above import is always required and I've never tried testing without it being in the spring context. What is the exception trace ? Perhaps the JAXRS interceptors are not even involved without the import ?

cheers, Sergey

Sergey Beryozkin
A: 

It turns out I was wrong: The problem was actually with the XSD: I had an XSD element "searchRequest" that is of type "SearchRequest" (sic, capital S), and additionally another root element that is using an extended type, derived from SearchRequest. It seems that cxf is having problems with a type that is used both as a root element's type and as a type for XSD inheritance. After creating an additional type AbstractSearchRequest and having all types inherit from that type the problem went away.