### 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 Registry getRegistry();  }### Answer:
@Test public void testGetRegistry() throws Exception { MessageContext context = new MessageContext(); MessageContext.setCurrentMessageContext(context); HttpServletRequest httpRequest = mock(HttpServletRequest.class); HttpSession httpSession = mock(HttpSession.class); when(httpSession.getAttribute(RegistryConstants.USER_REGISTRY)).thenReturn(userRegistry); when(httpRequest.getSession()).thenReturn(httpSession); context.setProperty("transport.http.servletRequest", httpRequest); assertEquals(userRegistry, Utils.getRegistry()); }
@Test(expected = RegistryException.class) public void testGetRegistryWithoutMC() throws Exception { MessageContext.setCurrentMessageContext(null); assertEquals(userRegistry, Utils.getRegistry()); }
@Test public void testGetRegistryFromService() throws Exception { MessageContext context = new MessageContext(); MessageContext.setCurrentMessageContext(context); HttpServletRequest httpRequest = mock(HttpServletRequest.class); HttpSession httpSession = mock(HttpSession.class); when(httpSession.getAttribute(RegistryConstants.USER_REGISTRY)).thenReturn(null); when(httpRequest.getSession()).thenReturn(httpSession); context.setProperty("transport.http.servletRequest", httpRequest); assertEquals(userRegistry, Utils.getRegistry()); }