Unit testing a ServerResource
// 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 the router
and the resource are @Autowired in my test class. The relevant declarations in the Spring application context looks 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 the myApplication
is similar to
public class MyApplication extends Application {
}