Intercepting Context.updateSearchIndex() in a unit test to throw an exception

I am working on a new controller method that calls Context.updateSearchIndex(), and I would like to write a unit test for when Context.updateSearchIndex() throws an exception. Is there an example in the openmrs codebase of a way to intercept a Context static method and throw an exception or otherwise set the outcome?

Hi Clif,

Take a look at this:

In particular, look at how Context.getAuthenticatedUser() is mocked to set the outcome.

mockStatic(Context.class);
when(Context.getAuthenticatedUser()).thenReturn(new User());
1 Like

https://wiki.openmrs.org/display/docs/Mock+Doc explains a cleaner approach.

To mock Context.updateSearchIndex() which delegates to getContextDAO().updateSearchIndex() you simply mock contextDAO and call Context.setDAO(mockedContextDAO).

1 Like

Thanks to both of you for your replies. They were helpful.

1 Like