Hi

The local transport for proxy-based JAX-RS clients will also be supported as soon as the client runtime becomes 'free' from HttpURLConnection references.

Cheers, Sergey

On 01/02/12 20:21, KARR, DAVID wrote:
-----Original Message-----
From: cganesan [mailto:[email protected]]
Sent: Wednesday, February 01, 2012 11:42 AM
To: [email protected]
Subject: Unit testing JAX-RS services

Hi

Is there a way to invoke the URLs for a JAX-RS end point services as
part of
JUnit test - triggered from MAven build? Does Apache CXF support
something
like an embedded container towards this?

Yes.  The core of this support is the "org.apache.cxf.jaxrs.JAXRSServerFactoryBean" 
class.  It internally manages an embedded Jetty server that your service is automatically deployed 
to.  You can then use the "WebClient" class to connect to the server and get a response.

You'll probably find more history on this topic by searching the mailing list 
(some of it from me).

What's somewhat annoying is that you have to manually (as far as I can tell) set the parameters for 
your JAX-RS server (which that class is defining), instead of just using the settings from the 
Spring context.  I've defined a "ServerFactory" class with a "create()" method 
that sets the properties that I need.  I'll show that method here, but note that I'm only setting 
properties that my app uses.  You may have other needs.

     public static void create(Object serviceBean, JSONProvider jsonProvider, 
String uri) {
         JAXRSServerFactoryBean  sf  = new JAXRSServerFactoryBean();

         BindingFactoryManager   manager = 
sf.getBus().getExtension(BindingFactoryManager.class);
         JAXRSBindingFactory     factory = new JAXRSBindingFactory();
         factory.setBus(sf.getBus());
         manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, 
factory);

         if (jsonProvider == null) {
             Map<String, String>  namespaceMap    = new HashMap<String, 
String>();
             XmlSchema   xmlSchemaAnnotation     = 
ServiceCallResults.class.getPackage().getAnnotation(XmlSchema.class);
             namespaceMap.put(xmlSchemaAnnotation.namespace(), "cns");
             jsonProvider    = new JSONProvider();
             jsonProvider.setNamespaceMap(namespaceMap);
             jsonProvider.setIgnoreNamespaces(false);
         }

         sf.setProvider(jsonProvider);

         setJSONProvider(jsonProvider);

         Map<Object, Object>  extensionsMap   = new HashMap<Object, Object>();
         extensionsMap.put("json", "application/json");
         extensionsMap.put("xml", "application/xml");

         sf.setExtensionMappings(extensionsMap);

         sf.setServiceBean(serviceBean);
         sf.getInInterceptors().add(new LoggingInInterceptor());
         sf.getOutInterceptors().add(new LoggingOutInterceptor());

         sf.setAddress(uri);

         sf.create();
     }

Using this, I can write an integration test (I don't call this a unit test) 
with a @BeforeClass method that initializes the server with my service beans, 
and then my test methods just use WebClient.



Reply via email to