IObjectSimpleImplPlugin implements IPlugin { @Override public void load() throws PluginException { try { IBootstrapItem<String> item = new BootstrapItem("IObjectSimpleImplPlugin"); item .after("IOC") .process(() -> { try { IKey fieldKey = Keys.getOrAdd(IObjectImpl.class.getCanonicalName()); IOC.register(fieldKey, new CreateNewInstanceStrategy( (args) -> new IObjectImpl() )); } catch (ResolutionException e) { throw new ActionExecuteException("IObjectSimpleImpl plugin can't load: can't get IObjectSimpleImpl key", e); } catch (InvalidArgumentException e) { throw new ActionExecuteException("IObjectSimpleImpl plugin can't load: can't create strategy", e); } catch (RegistrationException e) { throw new ActionExecuteException("IObjectSimpleImpl plugin can't load: can't register new strategy", e); } }); bootstrap.add(item); } catch (InvalidArgumentException e) { throw new PluginException("Can't load IObject plugin", e); } } IObjectSimpleImplPlugin(final IBootstrap<IBootstrapItem<String>> bootstrap); @Override void load();  }
@Test public void ShouldCorrectLoadPlugin() throws Exception { IKey IObjectKey = mock(IKey.class); when(Keys.getOrAdd(IObjectImpl.class.getCanonicalName())).thenReturn(IObjectKey); BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("IObjectSimpleImplPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("IObjectSimpleImplPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); ArgumentCaptor<CreateNewInstanceStrategy> createNewInstanceStrategyArgumentCaptor = ArgumentCaptor.forClass(CreateNewInstanceStrategy.class); actionArgumentCaptor.getValue().execute(); verifyStatic(); IOC.register(eq(IObjectKey), createNewInstanceStrategyArgumentCaptor.capture()); verify(bootstrap).add(bootstrapItem); }
@Test(expected = PluginException.class) public void ShouldThrowPluginException_When_BootstrapItemThrowsException() throws Exception { whenNew(BootstrapItem.class).withArguments("IObjectSimpleImplPlugin").thenThrow(new InvalidArgumentException("")); plugin.load(); }
@Test(expected = ActionExecuteException.class) public void ShouldThrowRuntimeException_When_LambdaThrowsException() throws Exception { when(Keys.getOrAdd(IObjectImpl.class.getCanonicalName())).thenThrow(new ResolutionException("")); BootstrapItem bootstrapItem = mock(BootstrapItem.class); whenNew(BootstrapItem.class).withArguments("IObjectSimpleImplPlugin").thenReturn(bootstrapItem); when(bootstrapItem.after(anyString())).thenReturn(bootstrapItem); plugin.load(); verifyNew(BootstrapItem.class).withArguments("IObjectSimpleImplPlugin"); ArgumentCaptor<IPoorAction> actionArgumentCaptor = ArgumentCaptor.forClass(IPoorAction.class); verify(bootstrapItem).process(actionArgumentCaptor.capture()); actionArgumentCaptor.getValue().execute(); }