@Override protected void prepareGraph(MvcComponent overriddingComponent) throws Exception { super.prepareGraph(overriddingComponent); overriddingComponent.register(new Object(){ /** * Mock resource service * @return */ @Provides public ResourceService resourceService() { resourceServiceMock = mock(ResourceService.class); return resourceServiceMock; } /** * Prepare objects to mock http calls * @return * @throws IOException */ @Provides public ServiceFactory serviceFactory() throws IOException { ipServiceCallMock = mock(Call.class); IpService ipServiceMock = mock(IpService.class); when(ipServiceMock.getIp(anyString())).thenReturn(ipServiceCallMock); ServiceFactory serviceFactoryMock = mock(ServiceFactory.class); when(serviceFactoryMock.createService(IpService.class)).thenReturn(ipServiceMock); return serviceFactoryMock; } }); }
@Test (expected = IllegalStateException.class) public void should_throw_out_exceptions_when_registering_component() throws ProvideException, ProviderConflictException, Graph.IllegalRootComponentException { // Arrange MvcComponent badComponent = mock(MvcComponent.class); MvcGraph mvcGraph = new MvcGraph(); mvcGraph.setRootComponent(badComponent); Object obj = new Object(); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { throw new IllegalStateException(); } }).when(badComponent).register(any(Object.class)); // Act mvcGraph.getRootComponent().register(obj); }
register(provider); } catch (ProviderConflictException e) {
@Test public void testInjectionOfMockController() throws Exception { final PrintController mockPrintController = mock(PrintController.class); graph.getRootComponent().register(new Provider<PrintController>(PrintController.class) { @Override protected PrintController createInstance() throws ProvideException { return mockPrintController; } }); TestView testView = new TestView(); graph.inject(testView); testView.present(); verify(mockPrintController, times(1)).print(); }
@Test public void should_throw_MvcGraphException_when_mvcGraph_destroy_method_encounters_PokeException() throws ProvideException, ProviderConflictException, ProviderMissingException { navigationManager.logger = mock(Logger.class); Navigator navigator = navigationManager.navigate(this); Mvc.graph().getRootComponent().register(new Object(){ @Provides public X x() { return new X_1(); } }); navigator.with(X.class); Mvc.graph().getRootComponent().unregister(X.class, null); navigator.destroy(); verify(navigationManager.logger).warn(anyString(), anyString()); }
@Test public void testOnDisposeShouldBeCalledWhenControllerReleased() throws Exception { final LifeCycleTestController.Proxy lifeCycleProxy = mock(LifeCycleTestController.Proxy.class); graph.getRootComponent().register(new LifeCycleTestControllerModule(lifeCycleProxy)); TestLifCycleView testView = new TestLifCycleView(); verify(lifeCycleProxy, times(0)).onCreated(); graph.inject(testView); verify(lifeCycleProxy, times(1)).onCreated(); verify(lifeCycleProxy, times(0)).onDestroy(); TestLifCycleView testView1 = new TestLifCycleView(); graph.inject(testView1); //Should be 1 still since a cached instance will be reused. verify(lifeCycleProxy, times(1)).onCreated(); verify(lifeCycleProxy, times(0)).onDestroy(); graph.release(testView1); verify(lifeCycleProxy, times(0)).onDestroy(); graph.release(testView); verify(lifeCycleProxy, times(1)).onDestroy(); }
component.register(new Object(){ @Provides UiThreadRunner uiThreadRunner() {
@Test public void use_method_should_retain_and_release_instance_correctly() throws ProvideException, ProviderConflictException { graph.getRootComponent().register(new DeviceModule()); do_use_method_should_retain_and_release_instance_correctly(); }
graph.setRootComponent((MvcComponent) new MvcComponent("TestRootComponent").register(new Object(){ @Provides @EventBusC
@Before public void setUp() throws Exception { graph = new MvcGraph(); graph.setRootComponent((MvcComponent) new MvcComponent("Root").register(new Object() { @Provides @EventBusC public EventBus eventBusC() { return new EventBusImpl(); } @Provides @EventBusV public EventBus eventBusV() { return new EventBusImpl(); } @Provides public ExecutorService provideExe() { return mock(ExecutorService.class); } })); }
graph.setRootComponent((MvcComponent) new MvcComponent("Root").register(new Object() { @Provides @EventBusC
@Test public void should_throw_ProviderConflictException_on_graph_preparation_by_duplicate_registration() throws Component.MultiParentException, ProviderConflictException, ProvideException { MvcComponent component = new MvcComponent(""); component.register(new Object() { @Provides public UiThreadRunner uiThreadRunner() { return mock(UiThreadRunner.class); } }); boolean caught = false; try { graph.prepareInternalGraph(new Graph(), component); } catch (MvcGraphException e) { if (e.getCause() instanceof ProviderConflictException) { caught = true; } } Assert.assertTrue(caught); }
@Test(expected = MvcGraphException.class) public void should_catch_invocation_exception_when_NPE_detected_on_injection() throws Exception { Object com = new Object() { @Provides @Singleton @Slower2 TimerController timerSlowerX2() { return new TimerController() { { onCreated(); } @Override public void setInitialValue(long value) { super.setInitialValue(value * 2); } }; } }; graph.getRootComponent().register(com); Annotation slower2Qualifier = SlowXHolder.class.getAnnotation(Slower2.class); graph.use(TimerController.class, slower2Qualifier, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { //Controller should have now been released Assert.assertEquals(0, instance.getInitialValue()); } }); graph.getRootComponent().unregister(com); }
overridingComponent.register(new Object(){ @Provides @EventBusC
@Test public void controllerShouldBeOverrideAfterExplicitlyRegistration() throws ProvideException, ProviderConflictException, CircularDependenciesException, ProviderMissingException { graph.getRootComponent().register(new PrinterModule(new MockPrinter())); TestView testView = new TestView(); graph.inject(testView); Assert.assertEquals(testView.printController.getClass(), MockPrinter.class); Assert.assertEquals(testView.printController.print(), ((MockPrinter)testView.printController).printContent); }
testComponent.register(new Object(){ @Provides public Engine v8Engine() {
@Test public void should_be_able_to_reinject_new_instance_without_default_constructor() throws ProvideException, ProviderConflictException, ProviderMissingException, CircularDependenciesException { AndroidModule component = new AndroidModule(); graph.getRootComponent().register(component); AndroidView androidView = new AndroidView(); Assert.assertNull(androidView.androidPart); graph.inject(androidView); Assert.assertNotNull(androidView.androidPart); graph.release(androidView); graph.inject(androidView); Assert.assertNotNull(androidView.androidPart); }
@Test public void shouldRecoverDefaultUsingDefaultImplAfterExplicitlyUnregistration() throws ProvideException, ProviderConflictException, CircularDependenciesException, ProviderMissingException { PrinterModule component = new PrinterModule(new MockPrinter()); graph.getRootComponent().register(component); TestView testView = new TestView(); graph.inject(testView); Assert.assertEquals(testView.printController.getClass(), MockPrinter.class); Assert.assertEquals(testView.printController.print(), ((MockPrinter)testView.printController).printContent); graph.getRootComponent().unregister(component); TestView testView1 = new TestView(); graph.inject(testView1); Assert.assertEquals(testView1.printController.getClass(), PrintController.class); PrintModel printModel = new PrintModel(); printModel.setContent("Hello Poke"); testView1.printController.bindModel(printModel); Assert.assertEquals(testView1.printController.print(), testView1.printController.getModel().getContent()); }