battling to get real object injected

140 views
Skip to first unread message

Marius Kruger

unread,
Feb 25, 2013, 9:49:10 AM2/25/13
to juk...@googlegroups.com
Hi,

Because gwt's NumberFormat can not be used in jukito tests, I'm trying to inject it
with one implementation when running in the browser and another for running in unit tests and possibly in server-side code.

As far as I can tell a mock is injected and I can't force it to inject a real object.

I inject it in my presenter's constructor:

@Inject
    public InputTabPresenter(final EventBus eventBus, final MyView view,
        final MyProxy proxy, IGwtNumberFormatSux format
    )
...


I tried to manually bind it and even to forceMock it:

    public abstract class AJukitoModule extends JukitoModule {
        /**
         * This is the test binder extending `MockingBinder` and
         * implementing the view's `Binder`.
         */
        static class MyTestBinder extends MockingBinder<Widget, InputTabView>
            implements Binder
        {
            @Inject
            public MyTestBinder(final MockitoMockFactory mockFactory) {
                super(Widget.class, mockFactory);
            }
    
        }
    
        @Override
        protected void configureTest() {
            GWTMockUtilities.disarm();
            bind(Binder.class).to(AJukitoModule.MyTestBinder.class);
            bind(IGwtNumberFormatSux.class).to(JavaNumberFormat.class);

            // forceMock(IGwtNumberFormatSux.class);
            // bindSpy(JavaNumberFormat.class);
        }
    }


But it never seems to call my mock method with IGwtNumberFormatSux.class :

public class MockitoMockFactory implements MockFactory {
    public <T> T mock(Class<T> classToMock) {


Any hints will be much appreciated.
I attached a zip of an example maven/eclipse project demonstrating my problem.
The unit test fails because the format method returns null because it is mocked :(.

-- 
thanks
✝ Marius
GwtpWithJukito.zip

Philippe Beaudoin

unread,
Feb 25, 2013, 10:53:37 AM2/25/13
to juk...@googlegroups.com, Christian Goudreau
I did not write gwtp's MockingBinder (Christan did) but looking at it, it just calls MockFactory.mock() with the type of the @UiField ignoring any guice binding that you may have done. The source is here:

I don't think the MockingBinder's mock factory is what you want to use in this case. Maybe more a provider of IGwtNumberFormatSux that provides the mock you want. Bind that as test singleton to make sure you have the same mock everywhere inside a test.

Cheers,

   Philippe


thanks
✝ Marius

--
You received this message because you are subscribed to the Google Groups "Jukito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jukito+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Christian Goudreau

unread,
Feb 25, 2013, 11:00:59 AM2/25/13
to Jukito
Using Mocking binder, any UIFields are already mocked using the mocking framework of your choice which is why it's part of GWTP as opposed to Jukito. It has no dependency on Jukito. MockingBinder is for Views, not presenters and using it removes the need to create a custom mock for your usage as you can then use Mockito to forward the result you want when calling a function.

Also a requirements that you need to remember when testing views, use composition over inheritance and to not extends composite, else the test will fail with or without mocking binder.
--
Christian Goudreau

Marius Kruger

unread,
Feb 25, 2013, 12:34:48 PM2/25/13
to juk...@googlegroups.com
thanks for the info guys, all is working now.
My current issue was that I forgot to add this:
    public static class Module extends AJukitoModule {
    }
to my test. 
I tried so many things, but I'm pretty sure it was failing in one of my other tests even with the above. 
I'm still trying to figure out what my other issue was.

-- 
this is what happen when still coding at 4am
✝ Marius
Reply all
Reply to author
Forward
0 new messages