Re: Re: Can I call my Restlet classes without a Web container?

2007-06-01 Thread Bryan Loofbourrow
 For example:

Thanks, this is just what I was looking for. The sample code is much
appreciated. I'll give it a whirl.

I am certainly in agreement with the wisdom of separating business logic from
the vehicle used to convey it, where feasible. I'm dealing with existing code
here -- but also there are things that are interesting to test other than the
underlying business logic, such as proper handling of the parameters to a
restlet. Unit testing the Restlet itself thing seems like a value, although I
suppose the ideal isolation would consist of unit testing the Restlet, with the
Restlet invoking a mock for the business logic. 



RE: Re: Can I call my Restlet classes without a Web container?

2007-05-31 Thread Chuck Hinson
As Adam points out, it is a good idea to keep the bulk of your business
logic outside your restlet classes for testing purposes.  However, we've
found it very easy to directly invoke various restlet components for
testing purposes - mostly to check return codes and ensure content
negotiation stuff is being handled properly.  For example:

public void testDelete()
{
Context context = new Context();
context.getAttributes().put(...stuff...);
Request request = new
Request(Method.DELETE,http://host/entries/+entryId);
Response response = new Response(request);
ContextEntryResource resource = new
ContextEntryResource(context,request,response);
resource.delete();

assertEquals(response.getStatus(),Status.SUCCESS_NO_CONTENT);

  assertTrue(...other tests to ensure delete worked
properly...);
}

where ContextEntryResource is defined as:

public class ContextEntryResource extends Resource 
{
public ContextEntryResource(Context context, Request request,
Response response) {
super(context, request, response);

}
public boolean allowDelete(){
return true;
}
public void delete(){
. . .
}
. . .
}


--Chuck 

-Original Message-
From: Adam Taft [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, May 30, 2007 9:13 PM
To: discuss@restlet.tigris.org
Subject: Re: Can I call my Restlet classes without a Web container?

Bryan,

I know this isn't directly answering your question, but how we do
testing is by integrating with the Spring Framework.  With Spring, you
get various DAO and service layer support classes which are easily
testable.

Essentially, if you make your Restlets as dumb as possible by
transferring all the business logic code into a Spring managed bean,
then you can isolate and test these beans and not need to generate mock
Request/Response objects.

 From there, if you want to test the whole stack, you can do so by using
the Jakarta Commons Http Client or other types of web testing
frameworks.

All of these tests can run as JUnit tests, so you can leverage its
testing support.

I'm sure others will have more ideas about how to mock a Request or
Response object like you're looking to do.  So, the above is just one
of, I'm sure, dozens of ideas.

Hope this helps,

Adam


Bryan Loofbourrow wrote:
 I'm interested in adding a unit test for certain aspects of the 
 behavior of my REST service, one that can execute outside the confines

 of a Web server. It would be sufficient if there were merely a way for

 me to call the Restlet classes directly, then examine the response.
 
 What's not obvious to me is how one would go about this. I'd need to 
 create Request and Response objects, obviously, but what's the best 
 way to do that, and what would I need to put in there? Is there a 
 Factory somewhere I should be using for this? Or is this the wrong 
 approach; is there some other way to accomplish this (short of 
 decoupling the code underlying the Restlet and testing that by
itself)?
 
 Thanks,
 
 -- Bryan


Re: Can I call my Restlet classes without a Web container?

2007-05-31 Thread Jim Alateras
I've done the same but test through the the Router since i'm also 
interested that the request gets routed to the appropriate resource.


Some of my resources talk to a DAO so i am thinking of using something 
like EasyMock [1] to isolate my resource testing.


cheers
/jima

[1] http://www.easymock.org/

Chuck Hinson wrote:

As Adam points out, it is a good idea to keep the bulk of your business
logic outside your restlet classes for testing purposes.  However, we've
found it very easy to directly invoke various restlet components for
testing purposes - mostly to check return codes and ensure content
negotiation stuff is being handled properly.  For example:

public void testDelete()
{
Context context = new Context();
context.getAttributes().put(...stuff...);
Request request = new
Request(Method.DELETE,http://host/entries/+entryId);
Response response = new Response(request);
ContextEntryResource resource = new
ContextEntryResource(context,request,response);
resource.delete();

assertEquals(response.getStatus(),Status.SUCCESS_NO_CONTENT);


  assertTrue(...other tests to ensure delete worked
properly...);
}

where ContextEntryResource is defined as:

public class ContextEntryResource extends Resource 
{

public ContextEntryResource(Context context, Request request,
Response response) {
super(context, request, response);

}

public boolean allowDelete(){
return true;
}
public void delete(){
. . .
}
. . .
}


--Chuck 


-Original Message-
From: Adam Taft [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, May 30, 2007 9:13 PM

To: discuss@restlet.tigris.org
Subject: Re: Can I call my Restlet classes without a Web container?

Bryan,

I know this isn't directly answering your question, but how we do
testing is by integrating with the Spring Framework.  With Spring, you
get various DAO and service layer support classes which are easily
testable.

Essentially, if you make your Restlets as dumb as possible by
transferring all the business logic code into a Spring managed bean,
then you can isolate and test these beans and not need to generate mock
Request/Response objects.

 From there, if you want to test the whole stack, you can do so by using
the Jakarta Commons Http Client or other types of web testing
frameworks.

All of these tests can run as JUnit tests, so you can leverage its
testing support.

I'm sure others will have more ideas about how to mock a Request or
Response object like you're looking to do.  So, the above is just one
of, I'm sure, dozens of ideas.

Hope this helps,

Adam


Bryan Loofbourrow wrote:
I'm interested in adding a unit test for certain aspects of the 
behavior of my REST service, one that can execute outside the confines



of a Web server. It would be sufficient if there were merely a way for



me to call the Restlet classes directly, then examine the response.

What's not obvious to me is how one would go about this. I'd need to 
create Request and Response objects, obviously, but what's the best 
way to do that, and what would I need to put in there? Is there a 
Factory somewhere I should be using for this? Or is this the wrong 
approach; is there some other way to accomplish this (short of 
decoupling the code underlying the Restlet and testing that by

itself)?

Thanks,

-- Bryan




Re: Can I call my Restlet classes without a Web container?

2007-05-30 Thread Adam Taft

Bryan,

I know this isn't directly answering your question, but how we do 
testing is by integrating with the Spring Framework.  With Spring, you 
get various DAO and service layer support classes which are easily testable.


Essentially, if you make your Restlets as dumb as possible by 
transferring all the business logic code into a Spring managed bean, 
then you can isolate and test these beans and not need to generate mock 
Request/Response objects.


From there, if you want to test the whole stack, you can do so by using 
the Jakarta Commons Http Client or other types of web testing frameworks.


All of these tests can run as JUnit tests, so you can leverage its 
testing support.


I'm sure others will have more ideas about how to mock a Request or 
Response object like you're looking to do.  So, the above is just one 
of, I'm sure, dozens of ideas.


Hope this helps,

Adam


Bryan Loofbourrow wrote:

I'm interested in adding a unit test for certain aspects of the behavior of my
REST service, one that can execute outside the confines of a Web server. It
would be sufficient if there were merely a way for me to call the Restlet
classes directly, then examine the response.

What's not obvious to me is how one would go about this. I'd need to create
Request and Response objects, obviously, but what's the best way to do that, and
what would I need to put in there? Is there a Factory somewhere I should be
using for this? Or is this the wrong approach; is there some other way to
accomplish this (short of decoupling the code underlying the Restlet and testing
that by itself)?

Thanks,

-- Bryan