Failing to Mock an interface

137 views
Skip to first unread message

David Parish

unread,
Sep 4, 2013, 2:44:18 PM9/4/13
to juk...@googlegroups.com
I use GWT, MVP and GIN.  I've setup Jukito because I want it to mock my views (interfaces). It's not working (it's creating the ConsoleImpl per the example below) and I have a few theories.  Mainly does Jukito ignore @ImplementedBy or use that instead of creating a mock? 

For instance here is a typical view:

@ImplementedBy(ConsoleImpl.class)
public interface ConsoleView extends View {
...
}

I'm not using the JukitoRunner instead I'm manually calling the JukitoModule:

public class TestInjectModule extends JukitoModule {

    @Override
    protected void configureTest() {
        bind(ConsoleView.Presenter.class).to(ConsolePresenter.class);
        bind(EventBus.class).to(CountingEventBus.class).asEagerSingleton();
    }
}

In the above example, ConsoleView is injected as a part of the ConsolePresenter constructor.

If I understand Jukito correctly it is supposed to see that an interface is requested and to mock out the interface. Is this correct or am I completely misunderstanding the project?

Thank you,
-David Parish

David Parish

unread,
Sep 5, 2013, 11:31:10 AM9/5/13
to juk...@googlegroups.com
I got this to work.  My problem was that I was not using the JukitoRunner (we use Play framework so I don't think I can).  

I had to do some sneaky stuff to get this to do what I want. I created my own module that exted JukitoModule and sadly the constructor for BindingsCollector is package protected so I had to put this class in an org.jukito package. Here's my custom module that does what I needed.

/**
 *
 * I had to put this in the org.jukito package because the Constructor for BindingsCollector is
 * package protected.
 */
public class SandboxTestModule extends JukitoModule {
    @Override
    protected void configureTest() {
        bind(MainView.Presenter.class).to(MainPresenter.class);
    }

    @Provides
    @Singleton
    MainView mainView() {
        MainView view =  org.mockito.Mockito.mock(MainView.class);
        HasWidgets contentPanel = org.mockito.Mockito.mock(HasWidgets.class);
        org.mockito.Mockito.when(view.getContentPanel()).thenReturn(contentPanel);
        return view;
    }

    // This is mimicking what JukitoTestRunner does so we can test w/o the runner.
    public SandboxTestModule() {
        BindingsCollector collector = new BindingsCollector(this);
        collector.collectBindings();
        setBindingsObserved(collector.getBindingsObserved());
    }

}

If JukitoModule's binding intialization was moved out of JukitoRunner and instead was a method in JukitoModule than this would have been easier. Something like this:

    public initBindings() {
        BindingsCollector collector = new BindingsCollector(this);
        collector.collectBindings();
        setBindingsObserved(collector.getBindingsObserved());
    }

Then JukitoRunner would change:

            BindingsCollector collector = new BindingsCollector(testModuleForCollection);
            collector.collectBindings();
            jukitoModule.setBindingsObserved(collector.getBindingsObserved());

to:
            jukitoModule.initBindings();

Jean-Philippe Poulin de Courval

unread,
Oct 16, 2013, 11:18:26 PM10/16/13
to juk...@googlegroups.com
Hi David,

I try to add Jukito to one of my Play project. Could you explain me how did you accomplish this? Currently, I tried to add this line in my Build.scala : "org.jukito" % "jukito" % "1.2" % "test" but without any success.

Obviously, I run play clean, play dependencies, play update, play idea (I use intellij) and play compile but still nothing.

What am I missing?

Thank you!

Jean-Philippe Poulin de Courval

unread,
Oct 16, 2013, 11:49:02 PM10/16/13
to juk...@googlegroups.com
Never mind, I forgot to add Maven Central repository using http://repo1.maven.org/maven/.

That was it!

David Parish

unread,
Oct 17, 2013, 7:01:03 PM10/17/13
to juk...@googlegroups.com
Glad you got it. I wish I could use JukitoRunner. I keep getting mocks that don't reset so there's still something wrong going on and I never see it when I use JukitoRunner. Also JukitoRunner resets the mocks on each run.

-Dave



--
You received this message because you are subscribed to a topic in the Google Groups "Jukito" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jukito/B84kf10iTWU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jukito+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Stephan Classen

unread,
Oct 17, 2013, 7:16:26 PM10/17/13
to juk...@googlegroups.com
resetting is done with a special scope of jukito.
It is called TestScope and offers a method clear() which will clear all mocks.

Mocks and spys which are binded using the methods bindMock() and bindSpy() are bound in the TestScope.
You can add other bindings by using bind(Xxx.class).to(XxxImpl.class).in(TestSingelton.class).

hope this helps
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.
Reply all
Reply to author
Forward
0 new messages