How to inject a real (non mocked) instance

430 views
Skip to first unread message

dindeman

unread,
Apr 24, 2011, 2:44:22 AM4/24/11
to Jukito
Hi Philippe and Christian.

Ok I was going for my first trials with Juckito.
I understand that the frameworks allows us to inject mocks easily and
this might sounds a silly question but... how do I then inject real
instances alongside with mocked ones?

For example I would like to inject a "real" DispatchAsync alongside
with a mocked view. How can I achieve this?

Thanks again for producing yet another amazing framework!
Cheers.
Luc

Philippe Beaudoin

unread,
Apr 24, 2011, 7:28:40 AM4/24/11
to juk...@googlegroups.com
Jukito lets you bind interfaces to whatever you want, so you can bind
your DispatchAsyncStub class to the DispatchAsync interface. The
biggest advantage of Jukito (compared to vanilla JUnit or Guiceberry)
is that it automatically binds any left-over unbound interfaces to
their Mockito mocks, plus in instantiates any class in the
TestSingleton scope so that everybody gets the same instance when a
test is running (plus a couple of other perks ;)).

In code, to do what you want, it will look a bit like this:

@RunWith(JukitoRunner.class)
public class MyPresenterTest {

public static class Module extends JukitoModule {
protected void configureTest() {
bind(DispatchAsync.class).to(DispatchAsyncStub.class).in(TestSingleton.class);
}
}

@Test
public void testAdd(MyPresenter sut, DispatchAsync dispatcher,
MyPresenter.MyView view) {
// ...

dindeman

unread,
Apr 25, 2011, 12:37:23 PM4/25/11
to Jukito
Hi Philippe.

Thanks a lot for the tip, it definitely helped me getting around the
new concepts behind Jukito.
I spent some trying to combine the use of Jukito's JukitoModule and
GWTP's MockHandlerModule.
Basically my issue was that I wanted my DispatchAsyncStub to take a
MockHandlerModule as a constructor's argument, so I needed
MockHandlerModule to be injected.

public class DispatchAsyncStub implements DispatchAsync {

private DispatchAsync dispatch;

@Inject
public DispatchAsyncStub(final MockHandlerModule mockHandlerModule) {
dispatch = Guice.createInjector(new DispatchModule(), new
ServerHandlerModule(),
mockHandlerModule).getInstance(DispatchAsync.class);
}
}

I wanted the above implementation of DispatchAsyncStub in order to
reuse it across various test units. But the server action handlers to
be bound would change from test unit to test unit, so I needed the
flexibility to pass a dedicated MockHandlerModule each time.

Then my JukitoModule looks something like this:

public static class Module extends JukitoModule {
private final ActionHandler actionHandler =
mock(ActionHandler.class);

@Provides
protected MockHandlerModule createMockHandlerModule() {
return new MockHandlerModule() {
@Override
protected void configureMockHandlers() {
bindMockActionHandler(ActionHandler.class, actionHandler);
}
};
}

@Provides
protected ActionHandler createLoginHandler() {
return actionHandler;
}
}

... where there is this one mock (actionHandler) that I have to
explicitly mock via the original Mockito function. This is because I
need to manipulate it already inside the JukitoModule. And that is
because the provider for MockHandlerModule is a method of the
JukitoModule.
Then obviously ActionHandler can be injected anywhere I want,
including inside the setup routine (@Before) in order to stub it. This
injection occurs smoothly thanks to the provider method that creates
the right one each time it's injected. The right one being the one
that was bound through bindMockActionHandler (whew.)

Well if you got some time to get into my post I'd be happy to know
whether this is the way to go, otherwise, hopefully, this will help
others during their first steps with Jukito.
Thumbs up for this new tool!

Luc

On Apr 24, 6:28 pm, Philippe Beaudoin <philippe.beaud...@gmail.com>
wrote:

Bhupesh Patel

unread,
Apr 24, 2012, 6:15:50 PM4/24/12
to juk...@googlegroups.com
Hello Dindeman,

I am trying to do something very similar to what you posted here. For me, I am trying to get actionResult object back from server from my unit test. I have crated mock handler and and mock DispatchAsync also. But when i step thru this my execution never stops at "onSucess()" of my presenter. Any idea how can i make this work?

Philippe Lhoste

unread,
Jun 13, 2013, 8:38:36 AM6/13/13
to juk...@googlegroups.com
I was puzzled because I tried this* but Jukito gave me a mock of my Mock class...

* =
bind(DocumentService.class).to(MockDocumentService.class).in(Singleton.class);

I finally found out that my MockDocumentService needed a default constructor (I just removed the useless constructor with parameters I had) or a constructor with no parameters...
It makes sense in retrospect, but it might not be obvious immediately, so I hope this message can help somebody... :-)

Regards.

Philippe Beaudoin

unread,
Jun 13, 2013, 8:43:03 AM6/13/13
to juk...@googlegroups.com

On an @Inject constructor. :)

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