You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Nayan Hajratwala <na...@chikli.com> on 2009/12/23 20:10:00 UTC

Unit Tests for CXF JSON services

Folks,

I am trying (without much success) to write a unit test against a CXF service. Note that by Unit Test, I mean no starting up a "dummy" web server, no network access, etc. I will be using mocks for these parts.

Essentially what I want to do is this:

1) Set up a JSON request, for simplicity:

{ "userId":1 }

2) Invoke the CXF service with that request to:
	
/services/getUser

During this step, i'll be injecting mocks into the service, so it will always return "Joe Schmoe"


3) Assert that the response comes back in the JSON format of:

{ getUserResponse: { "firstName":"Joe", "lastName":"Schmoe" } }


My problem is that outside of a deployed/running web instance, i'm not sure how to get step 2 to take in JSON and return JSON.

Suggestions?

---
Nayan Hajratwala
http://agileshrugged.com
http://twitter.com/nhajratw
734.658.6032


RE: Unit Tests for CXF JSON services

Posted by "KARR, DAVID (ATTCINW)" <dk...@att.com>.
> -----Original Message-----
> From: Nayan Hajratwala [mailto:nayan@chikli.com]
> Sent: Wednesday, December 23, 2009 11:10 AM
> To: users@cxf.apache.org
> Subject: Unit Tests for CXF JSON services
> 
> Folks,
> 
> I am trying (without much success) to write a unit test against a CXF
> service. Note that by Unit Test, I mean no starting up a "dummy" web
> server, no network access, etc. I will be using mocks for these parts.

I had asked about related issues a few days ago, with a subject line of
"Practical to write unit tests that can simulate the container and
verify XML/JSON data?".  I haven't fully explored the answer, but I
think it was a reasonable solution.

> 
> Essentially what I want to do is this:
> 
> 1) Set up a JSON request, for simplicity:
> 
> { "userId":1 }
> 
> 2) Invoke the CXF service with that request to:
> 
> /services/getUser
> 
> During this step, i'll be injecting mocks into the service, so it will
> always return "Joe Schmoe"
> 
> 
> 3) Assert that the response comes back in the JSON format of:
> 
> { getUserResponse: { "firstName":"Joe", "lastName":"Schmoe" } }
> 
> 
> My problem is that outside of a deployed/running web instance, i'm not
> sure how to get step 2 to take in JSON and return JSON.
> 
> Suggestions?
> 
> ---
> Nayan Hajratwala
> http://agileshrugged.com
> http://twitter.com/nhajratw
> 734.658.6032


Re: Unit Tests for CXF JSON services

Posted by Nayan Hajratwala <na...@chikli.com>.
Interesting approach, but i'm really looking for a way to do it without having to start up the server (in or out of process).

I may end up giving this a try failing something else.

On Dec 23, 2009, at 4:23 PM, Sergey Beryozkin wrote:

> Hi
> 
> Here is the one possible approach :
> http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/o
> rg/apache/cxf/systest/jaxrs/JAXRSLoggingAtomPushSpringTest.java
> 
> Typically a test server is launched in another process, but Andy was not
> keen on it :-) so he embedded an actual resource class inside
> JAXRSLoggingAtomPushSpringTest but I was happy to follow by adding more
> resources inside too...
> You can use WebClient to test these embedded resources, the
> corresponding spring config is here :
> 
> http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resour
> ces/jaxrs_logging_atompush/WEB-INF/beans.xml
> 
> see jaxrs:server there.
> 
> 
> I'm about to sign off but I'll be happy to provide more info later on
> Cheers, Sergey 
> 
> -----Original Message-----
> From: Nayan Hajratwala [mailto:nayan@chikli.com] 
> Sent: 23 December 2009 19:10
> To: users@cxf.apache.org
> Subject: Unit Tests for CXF JSON services
> 
> Folks,
> 
> I am trying (without much success) to write a unit test against a CXF
> service. Note that by Unit Test, I mean no starting up a "dummy" web
> server, no network access, etc. I will be using mocks for these parts.
> 
> Essentially what I want to do is this:
> 
> 1) Set up a JSON request, for simplicity:
> 
> { "userId":1 }
> 
> 2) Invoke the CXF service with that request to:
> 	
> /services/getUser
> 
> During this step, i'll be injecting mocks into the service, so it will
> always return "Joe Schmoe"
> 
> 
> 3) Assert that the response comes back in the JSON format of:
> 
> { getUserResponse: { "firstName":"Joe", "lastName":"Schmoe" } }
> 
> 
> My problem is that outside of a deployed/running web instance, i'm not
> sure how to get step 2 to take in JSON and return JSON.
> 
> Suggestions?
> 
> ---
> Nayan Hajratwala
> http://agileshrugged.com
> http://twitter.com/nhajratw
> 734.658.6032
> 
> 


RE: Unit Tests for CXF JSON services

Posted by Sergey Beryozkin <sb...@progress.com>.
Hi

Here is the one possible approach :
http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/java/o
rg/apache/cxf/systest/jaxrs/JAXRSLoggingAtomPushSpringTest.java

Typically a test server is launched in another process, but Andy was not
keen on it :-) so he embedded an actual resource class inside
JAXRSLoggingAtomPushSpringTest but I was happy to follow by adding more
resources inside too...
You can use WebClient to test these embedded resources, the
corresponding spring config is here :

http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resour
ces/jaxrs_logging_atompush/WEB-INF/beans.xml

see jaxrs:server there.

  
I'm about to sign off but I'll be happy to provide more info later on
Cheers, Sergey 

-----Original Message-----
From: Nayan Hajratwala [mailto:nayan@chikli.com] 
Sent: 23 December 2009 19:10
To: users@cxf.apache.org
Subject: Unit Tests for CXF JSON services

Folks,

I am trying (without much success) to write a unit test against a CXF
service. Note that by Unit Test, I mean no starting up a "dummy" web
server, no network access, etc. I will be using mocks for these parts.

Essentially what I want to do is this:

1) Set up a JSON request, for simplicity:

{ "userId":1 }

2) Invoke the CXF service with that request to:
	
/services/getUser

During this step, i'll be injecting mocks into the service, so it will
always return "Joe Schmoe"


3) Assert that the response comes back in the JSON format of:

{ getUserResponse: { "firstName":"Joe", "lastName":"Schmoe" } }


My problem is that outside of a deployed/running web instance, i'm not
sure how to get step 2 to take in JSON and return JSON.

Suggestions?

---
Nayan Hajratwala
http://agileshrugged.com
http://twitter.com/nhajratw
734.658.6032