### Question:
ParamResourceFactory implements Factory<Object> { @Override public Object provide() { final Resource resource = extractFHIRResource(extractParameters()); final Class<?> rawType = parameter.getRawType(); try { return rawType.cast(resource); } catch (ClassCastException e) { logger.error("Parameter type does not match payload", e); throw new WebApplicationException(String.format("Provided resource must be: `%s`, not `%s`", rawType.getSimpleName(), resource.getResourceType()), Response.Status.BAD_REQUEST); } } ParamResourceFactory(Injector injector, Parameter parameter, IParser parser); @Override Object provide(); @Override void dispose(Object instance);  }### Answer:
@Test void testUnnamedResource() throws IOException { final Parameters parameters = new Parameters(); final Patient dummyPatient = new Patient(); parameters.addParameter().setResource(dummyPatient); final HttpServletRequest mock = Mockito.mock(HttpServletRequest.class); final ServletInputStream mockStream = Mockito.mock(ServletInputStream.class); final IParser parser = Mockito.mock(IParser.class); Mockito.when(parser.parseResource(Mockito.eq(Parameters.class), Mockito.any(InputStream.class))).thenReturn(parameters); final Injector mockInjector = Mockito.mock(Injector.class); Mockito.when(mockInjector.getInstance(HttpServletRequest.class)).thenReturn(mock); Mockito.when(mock.getInputStream()).thenReturn(mockStream); final Parameter parameter = Mockito.mock(Parameter.class); final FHIRParameter annotation = Mockito.mock(FHIRParameter.class); Mockito.when(annotation.name()).thenReturn(""); Mockito.when(parameter.getAnnotation(FHIRParameter.class)).thenReturn(annotation); Mockito.when(parameter.getRawType()).thenAnswer(answer -> Patient.class); final ParamResourceFactory factory = new ParamResourceFactory(mockInjector, parameter, parser); assertTrue(dummyPatient.equalsDeep((Patient) factory.provide()), "Should have returned dummy patient"); }
@Test void testNamedResource() throws IOException { final Parameters parameters = new Parameters(); final Patient unnamedPatient = new Patient(); final Patient namedPatient = new Patient(); namedPatient.addName().setFamily("Patient").addGiven("Test"); parameters.addParameter().setResource(unnamedPatient); parameters.addParameter().setResource(namedPatient).setName("named"); final HttpServletRequest mock = Mockito.mock(HttpServletRequest.class); final ServletInputStream mockStream = Mockito.mock(ServletInputStream.class); final IParser parser = Mockito.mock(IParser.class); Mockito.when(parser.parseResource(Mockito.eq(Parameters.class), Mockito.any(InputStream.class))).thenReturn(parameters); final Injector mockInjector = Mockito.mock(Injector.class); Mockito.when(mockInjector.getInstance(HttpServletRequest.class)).thenReturn(mock); Mockito.when(mock.getInputStream()).thenReturn(mockStream); final Parameter parameter = Mockito.mock(Parameter.class); final FHIRParameter annotation = Mockito.mock(FHIRParameter.class); Mockito.when(annotation.name()).thenReturn("named"); Mockito.when(parameter.getAnnotation(FHIRParameter.class)).thenReturn(annotation); Mockito.when(parameter.getRawType()).thenAnswer(answer -> Patient.class); final ParamResourceFactory factory = new ParamResourceFactory(mockInjector, parameter, parser); assertAll(() -> assertTrue(namedPatient.equalsDeep((Patient) factory.provide()), "Should have returned dummy patient"), () -> assertFalse(unnamedPatient.equalsDeep((Patient) factory.provide()), "Should have returned dummy patient")); }