> -----Original Message----- > From: Sergey Beryozkin [mailto:[email protected]] > Sent: Thursday, October 20, 2011 2:47 PM > To: [email protected] > Subject: Re: unit testing REST services > > Hi > > On 20/10/11 17:52, Guy Pardon wrote: > > Hi, > > > > With JAXWS/SOAP, we can unit test services as explained in > http://www.jroller.com/gmazza/entry/junit_web_service_testing > > > > Does something similar exist for JAXRS/REST? > > We can't do the local transport testing just yet though there've been > an > enhancement request - that is certainly possible to do with proxies - > but at the moment HTTPUrlConnection has not been completely removed yet > from the proxy code - we'll be there soon enough I guess once the > explicit support for async invocations gets implemented. > > Using embedded Jetty is easy enough, this post is a bit old but > captures > it well: > > http://aruld.info/cxf-22-in-action-services-design-simplified/ > > Spring Unit testing is also possible, David Karr has a lot of > experience > with it
A "lot" of experience is an exaggeration, but I've spent some time and thought carefully about what has to be achieved here. What I tend to do for automated testing of REST services is the following: * A set of true unit tests that consider the service class as a POJO, and mock its dependencies. * A set of "back-end" integration tests of the "service" layer, where "service" means the transactional service layer used by the REST service controller (the two interpretations of "service" can be confusing here). * A set of "front-end" integration tests, where the service layer is mocked, and the tests are verifying behavior in the REST controller and in the marshalling and unmarshalling behavior. Typically, the first scenario should handle most of the behavior in the REST controller itself, so the critical behavior being tested here is marshalling and unmarshalling. You would tend to use Mockito or something similar in the first and third scenario. The third scenario is likely what you're referring to, and that would use the JAXRSServerFactoryBean to control the embedded Jetty server. The first and third scenarios would also likely use the VerboseMockitoJUnitRunner (if you're using Mockito), but the second scenario is more likely to use SpringJUnit4ClassRunner. > > Cheers, Sergey > > > > > Thanks > > Guy
