REST: Unit testing a custom SearchHandler

Hi everyone,

I have just created a custom ObservationByOrderSearchHandler that retrieves observations by order. It works just fine.

Now I am writing the unit tests for it, based on what has been done in the ‘LocationSearchHandlerTest’.

So when I extend the MainResourceControllerTest class,

public class ObservationByOrderSearchHandlerTest extends MainResourceControllerTest {

I inherit of a bunch of other tests, among which the shouldGetAll test.

But this test fails. See the failure trace:

org.openmrs.module.webservices.rest.web.response.ResourceDoesNotSupportOperationException
	at org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource.doGetAll(DelegatingCrudResource.java:225)
	at org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource.getAll(DelegatingCrudResource.java:212)
	at org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceController.get(MainResourceController.java:176)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
	at org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest.handle(MainResourceControllerTest.java:155)
	at org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest.shouldGetAll(MainResourceControllerTest.java:202)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
	...

Any idea why ?


After a bit of research, It seems to me that the DelegatingCrudResource is looking for a doGetAll() method implementation in the Observation resource.

However this method does not exist in ObsResource1_8 (unlike in some other resources like LocationResource1_8#doGetAll() for instance ),

But I am not really sure what to do now.

Thanks

1 Like

In general doGetAll is not supported for objects, which implement OpenmrsData. It is supported for OpenmrsMetadata.

In your case just override shouldGetAll and annotate it with @Test(expected= ResourcesDoesNotSupportOperationException.class)

See https://github.com/openmrs/openmrs-module-webservices.rest/blob/master/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/ObsController1_9Test.java#L70

3 Likes

OK, done.

/**
 * @see org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest#shouldGetAll()
 */
@Override
@Test(expected = ResourceDoesNotSupportOperationException.class)
public void shouldGetAll() throws Exception {
    super.shouldGetAll();
}

Thank you @raff

1 Like

Had a similar problem, resolved it thanks to this discussion.

1 Like