0 Replies Latest reply: Dec 28, 2009 3:50 AM by 801419 RSS

    EasyMock for testing Dependency Injection

    801419
      Hello Suners,

      I have a problem when trying to test dependency injection using easyMock. i'm using GWT and HMVC and event collaboration infrastructure,

      I'm only testing presenters as it holds all logic that need to be tested but i don't have a certain way to test injection another presenters in the current one.

      The presenter's onBind method is like this:
      @Inject
           public ActiveEmployeesListPresenter(final Display display,
                     final EventBus eventBus, final DispatchAsync dispatcher) {
                super("Active Employee List", MenuPlace.P_MOD3.getPlace(), display,
                          eventBus, dispatcher);
                bind();
           }
      
           @Override
           protected void onBind() {
                buttonInjector = GWT.create(AddButtonInjector.class);
                buttonPresenter = buttonInjector.getAddButtonPresenter();
      
                display.getTitlePanel().add(buttonPresenter.getDisplay().asWidget());
      
                saveButtonEventHandler = eventBus.addHandler(SaveEmployeeEvent.TYPE,
                          new SaveButtonPressed());
                unRetireButtonEventHandler = eventBus.addHandler(
                          UnretireEmployeeEvent.TYPE, new UnretiredEmployeeHandler());
                retireButtonEventHandler = eventBus.addHandler(
                          RetireEmployeeEvent.TYPE, new RetiredEmployeeHandler());
                loadHandler = eventBus.addHandler(LoadActiveEmployeeEvent.TYPE,
                          new loadActiveDataHandler());
      
                loadData();
           }
      and this is how i'm testing it:
      @Before
           public void setUp() throws Exception {
                GWTMockUtilities.disarm();
                ActiveEmployeesListPresenterTestModule module = new ActiveEmployeesListPresenterTestModule(
                          ActiveEmployeesListPresenter.class);
                final Injector injector = Guice.createInjector(module);
      
                eventBus = injector.getInstance(EventBus.class);
                dispatcher = injector.getInstance(DispatchAsync.class);
                display = injector
                          .getInstance(ActiveEmployeesListPresenter.Display.class);
                
                
                 buttonInjector = injector.getInstance(AddButtonInjector.class);
                 EasyMock.expect(EasyMock.eq(buttonInjector.getAddButtonPresenter())).andReturn(isA(AddButtonPresenter.class));
      
                 // presenter.buttonInjector = injector
                // .getInstance(AddButtonInjector.class);
                // presenter.buttonPresenter = presenter.buttonInjector
                // .getAddButtonPresenter();
      
                MvpTestHelper.expectAddAnyHandler(eventBus, SaveEmployeeEvent.TYPE);
      
                MvpTestHelper.expectAddAnyHandler(eventBus, UnretireEmployeeEvent.TYPE);
      
                MvpTestHelper.expectAddAnyHandler(eventBus, RetireEmployeeEvent.TYPE);
      
                MvpTestHelper.expectAddAnyHandler(eventBus, LoadActiveEmployeeEvent.TYPE);
      
                EasyMock.replay(eventBus, dispatcher, display);
      
                presenter = injector
                          .getInstance(ActiveEmployeesListPresenter.class);
      
                EasyMock.verify(eventBus, dispatcher, display);
      
                assertEquals(presenter.buttonInjector, buttonInjector);
                assertEquals(presenter.buttonPresenter,buttonInjector.getAddButtonPresenter());
                assertEquals(dispatcher, presenter.getDispatcher());
                assertEquals(expectedPlace, presenter.getPlace());
                assertEquals("Page " + expectedPlace.getId(), presenter.getPageName());
      
                EasyMock.reset(eventBus, dispatcher, display);
      
           }
      
           @Test
           public void testSetup() {
      
           }
      When i run this test i get this error:

      java.lang.IllegalStateException: 2 matchers expected, 4 recorded.
           at org.easymock.internal.ExpectedInvocation.createMissingMatchers(ExpectedInvocation.java:56)
           at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:48)
           at org.easymock.internal.ExpectedInvocation.<init>(ExpectedInvocation.java:40)
           at org.easymock.internal.RecordState.invoke(RecordState.java:76)
           at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:38)
           at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:72)
           at $Proxy5.addHandler(Unknown Source)
           at com.bc.project.framwork.MvpTestHelper.expectAddAnyHandler(MvpTestHelper.java:340)
           at com.bc.project.client.modules.module3.page.widgets.lists.active.ActiveEmployeesListPresenterTest.setUp(ActiveEmployeesListPresenterTest.java:56)
           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
           at java.lang.reflect.Method.invoke(Method.java:597)
           at org.junit.internal.runners.BeforeAndAfterRunner.invokeMethod(BeforeAndAfterRunner.java:74)
           at org.junit.internal.runners.BeforeAndAfterRunner.runBefores(BeforeAndAfterRunner.java:50)
           at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:33)
           at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)
           at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)
           at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66)
           at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35)
           at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42)
           at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34)
           at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
           at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
           at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
           at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)