### Question:
Utils { public static Registry getRegistry() throws RegistryException { MessageContext messageContext = MessageContext.getCurrentMessageContext(); if (messageContext == null) { String msg = "Could not get the user's Registry session. Message context not found."; log.error(msg); throw new RegistryException(msg); } HttpServletRequest request = (HttpServletRequest) messageContext.getProperty("transport.http.servletRequest"); Registry registry = (Registry) request.getSession().getAttribute(RegistryConstants.USER_REGISTRY); if (registry == null) { String msg = "User's Registry instance is not found. " + "Creating a anonymous Registry instance for the user."; if(log.isDebugEnabled()) { log.debug(msg); } if (registryService == null) { msg = "Unable to create anonymous Registry instance for user. " + "Registry Service was not found."; log.error(msg); throw new RegistryException(msg); } registry = registryService.getUserRegistry(); request.getSession().setAttribute(RegistryConstants.USER_REGISTRY, registry); } return registry; }  static void setRegistryService(RegistryService service); static RegistryService getRegistryService(); static WaitBeforeShutdownObserver[] getWaitBeforeShutdownObservers(); static void setWaitBeforeShutdownObserver(WaitBeforeShutdownObserver service); static void clearWaitBeforeShutdownObserver(); static Registry getRegistry(); static String getDefaultEventingServiceURL(); static void setDefaultEventingServiceURL(String defaultEventingServiceURL); static String getRemoteTopicHeaderName(); static void setRemoteTopicHeaderName(String remoteTopicHeaderName); static String getRemoteTopicHeaderNS(); static void setRemoteTopicHeaderNS(String remoteTopicHeaderNS); static String getRemoteSubscriptionStoreContext(); static void setRemoteSubscriptionStoreContext(String remoteSubscriptionStoreContext); static boolean isIndexingConfigAvailable();  }### Answer:
@Test(expected = RegistryException.class) public void testGetRegistryWithoutMessageContext() throws RegistryException { Utils.getRegistry(); }
@Test public void testGetRegistry() throws RegistryException { MessageContext context = new MessageContext(); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpSession session = Mockito.mock(HttpSession.class); when(session.getAttribute(RegistryConstants.USER_REGISTRY)).thenReturn(null); when(request.getSession()).thenReturn(session); context.setProperty("transport.http.servletRequest", request); MessageContext.setCurrentMessageContext(context); RegistryService service = Mockito.mock(RegistryService.class); UserRegistry registry = Mockito.mock(UserRegistry.class); when(service.getUserRegistry()).thenReturn(registry); final Registry[] registries = {null}; doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); registries[0] = (Registry) args[1]; return null; } }).when(session).setAttribute(eq(RegistryConstants.USER_REGISTRY), (Registry)anyObject()); Utils.setRegistryService(service); Registry registry1 = Utils.getRegistry(); assertEquals(registry, registry1); assertEquals(registry, registries[0]); }
@Test(expected = RegistryException.class) public void testGetRegistryWithoutRegistryService() throws RegistryException { MessageContext context = new MessageContext(); HttpServletRequest request = Mockito.mock(HttpServletRequest.class); HttpSession session = Mockito.mock(HttpSession.class); when(session.getAttribute(RegistryConstants.USER_REGISTRY)).thenReturn(null); when(request.getSession()).thenReturn(session); context.setProperty("transport.http.servletRequest", request); MessageContext.setCurrentMessageContext(context); Utils.getRegistry(); }