CreateNewInstanceStrategy implements IResolveDependencyStrategy { public <T> T resolve(final Object ... args) throws ResolveDependencyStrategyException { try { return (T) creationFunction.apply(args); } catch (Exception e) { throw new ResolveDependencyStrategyException("Object resolution failed.", e); } } CreateNewInstanceStrategy(final Function<Object[], Object> func); T resolve(final Object ... args);  }
@Test public void checkStrategyCreation() throws Exception { Checker checker = new Checker(); Object value = new Object(); IResolveDependencyStrategy strategy = new CreateNewInstanceStrategy( (args) ->{ checker.wasCalled = true; return value; } ); Object result = strategy.resolve(); assertEquals(value, result); assertSame(value, result); assertTrue(checker.wasCalled); }
@Test public void checkStrategyCreationWithArgs() throws Exception { Checker checker = new Checker(); IResolveDependencyStrategy strategy = new CreateNewInstanceStrategy( (args) -> { checker.wasCalled = true; assertEquals(2, args.length); assertEquals(1, args[0]); assertEquals("test", args[1]); return null; } ); strategy.resolve(1, "test"); }
@Test (expected = ResolveDependencyStrategyException.class) public void checkResolveDependencyStrategyExceptionOnWrongArgs() throws Exception { IResolveDependencyStrategy strategy = new CreateNewInstanceStrategy( (args) -> { Integer a = (Integer) args[0]; return null; } ); strategy.resolve(); }