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

Reply via email to