Testing a ServerResource Device
// Code under test public class MyServerResource extends ServerResource { @Get public String getResource() { // ...... } } // Test code @Autowired private SpringBeanRouter router; @Autowired private MyServerResource myServerResource; String resourceUri = "/project/1234"; Request request = new Request(Method.GET, resourceUri); Response response = new Response(request); router.handle(request, response); assertEquals(200, response.getStatus().getCode()); assertTrue(response.isEntityAvailable()); assertEquals(MediaType.TEXT_PLAIN, response.getEntity().getMediaType()); String responseString = response.getEntityAsText(); assertNotNull(responseString);
where is the router and the @Autowired resource in my test class. Relevant declarations in the context of the Spring application look like
<bean name="router" class="org.restlet.ext.spring.SpringBeanRouter" /> <bean id="myApplication" class="com.example.MyApplication"> <property name="root" ref="router" /> </bean> <bean name="/project/{project_id}" class="com.example.MyServerResource" scope="prototype" autowire="byName" />
And myApplication is like
public class MyApplication extends Application { }
source share