@Test public void method_is_invoked_with_parameters_from_specimen_context() throws NoSuchMethodException { when(mockSpecimenContext.resolve(SpecimenType.of(String.class))).thenReturn("string"); when(mockSpecimenContext.resolve(SpecimenType.of(int.class))).thenReturn(1); Object specimen = this.relay.create(getRequest(), mockSpecimenContext); assertTrue(specimen instanceof TypeWithFactoryMethod); assertEquals("string", ((TypeWithFactoryMethod) specimen).getSymbol()); assertEquals(1, ((TypeWithFactoryMethod) specimen).getSize()); }
@Override public Object create(Object request, SpecimenContext context) { if(!(request instanceof FactoryMethodRequest)) { return new NoSpecimen(); } FactoryMethodRequest factoryMethodRequest = (FactoryMethodRequest)request; Method method = factoryMethodRequest.getMethod(); List<Object> parameters = ParameterUtils.getMethodParameters(method, factoryMethodRequest.getContainingType(), context); if(parameters == null) { return new NoSpecimen(); } try { Object[] paramsArray = parameters.toArray(new Object[parameters.size()]); return method.invoke(null, paramsArray); } catch (Exception e) { throw new ObjectCreationException(String.format("Unable to invoke factory method %s", method), e); } }
FactoryMethodRelay implements SpecimenBuilder { @Override public Object create(Object request, SpecimenContext context) { if(!(request instanceof FactoryMethodRequest)) { return new NoSpecimen(); } FactoryMethodRequest factoryMethodRequest = (FactoryMethodRequest)request; Method method = factoryMethodRequest.getMethod(); List<Object> parameters = ParameterUtils.getMethodParameters(method, factoryMethodRequest.getContainingType(), context); if(parameters == null) { return new NoSpecimen(); } try { Object[] paramsArray = parameters.toArray(new Object[parameters.size()]); return method.invoke(null, paramsArray); } catch (Exception e) { throw new ObjectCreationException(String.format("Unable to invoke factory method %s", method), e); } } }
FactoryMethodRelay implements SpecimenBuilder { @Override public Object create(Object request, SpecimenContext context) { if(!(request instanceof FactoryMethodRequest)) { return new NoSpecimen(); } FactoryMethodRequest factoryMethodRequest = (FactoryMethodRequest)request; Method method = factoryMethodRequest.getMethod(); List<Object> parameters = ParameterUtils.getMethodParameters(method, factoryMethodRequest.getContainingType(), context); if(parameters == null) { return new NoSpecimen(); } try { Object[] paramsArray = parameters.toArray(new Object[parameters.size()]); return method.invoke(null, paramsArray); } catch (Exception e) { throw new ObjectCreationException(String.format("Unable to invoke factory method %s", method), e); } }  }
FactoryMethodRelay implements SpecimenBuilder { @Override public Object create(Object request, SpecimenContext context) { if(!(request instanceof FactoryMethodRequest)) { return new NoSpecimen(); } FactoryMethodRequest factoryMethodRequest = (FactoryMethodRequest)request; Method method = factoryMethodRequest.getMethod(); List<Object> parameters = ParameterUtils.getMethodParameters(method, factoryMethodRequest.getContainingType(), context); if(parameters == null) { return new NoSpecimen(); } try { Object[] paramsArray = parameters.toArray(new Object[parameters.size()]); return method.invoke(null, paramsArray); } catch (Exception e) { throw new ObjectCreationException(String.format("Unable to invoke factory method %s", method), e); } }  @Override Object create(Object request, SpecimenContext context); }
FactoryMethodRelay implements SpecimenBuilder { @Override public Object create(Object request, SpecimenContext context) { if(!(request instanceof FactoryMethodRequest)) { return new NoSpecimen(); } FactoryMethodRequest factoryMethodRequest = (FactoryMethodRequest)request; Method method = factoryMethodRequest.getMethod(); List<Object> parameters = ParameterUtils.getMethodParameters(method, factoryMethodRequest.getContainingType(), context); if(parameters == null) { return new NoSpecimen(); } try { Object[] paramsArray = parameters.toArray(new Object[parameters.size()]); return method.invoke(null, paramsArray); } catch (Exception e) { throw new ObjectCreationException(String.format("Unable to invoke factory method %s", method), e); } }  @Override Object create(Object request, SpecimenContext context);  }