Guice + Mockito

2,619 views
Skip to first unread message

Ari King

unread,
Nov 4, 2013, 11:09:29 AM11/4/13
to google...@googlegroups.com
How can I mock injected dependencies in JUnit tests? Thanks.

Stephan Classen

unread,
Nov 4, 2013, 11:15:19 AM11/4/13
to google...@googlegroups.com
see: https://groups.google.com/forum/#!topic/google-guice/NEkahqnpY4k



On 11/04/2013 05:09 PM, Ari King wrote:
> How can I mock injected dependencies in JUnit tests? Thanks.
> --
> You received this message because you are subscribed to the Google
> Groups "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to google-guice...@googlegroups.com.
> To post to this group, send email to google...@googlegroups.com.
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/groups/opt_out.

Peter Reilly

unread,
Nov 4, 2013, 12:17:30 PM11/4/13
to google...@googlegroups.com
I use constructor injection and the injected objects are mockito objects.

To help things a little with using @Mock attributes I have a simple base test class:

public class MockedTester {
    public MockedTester() {
        MockitoAnnotations.initMocks(this);
    }
}

public class DomainBlackListTest extends MockedTester {
   @Mock public EventBus eb;
   @Mock public EventBusSingleton eventBus;

   @Before public void initMocks() {
       when(eventBus.getEventBus())
           .thenReturn(eb);
   }

   @Test public void simple() {
      DomainBlackList d = new DomainBlackList(eventBus);
      .....

   }

}


On Mon, Nov 4, 2013 at 9:15 AM, Stephan Classen <st.cl...@gmx.ch> wrote:




On 11/04/2013 05:09 PM, Ari King wrote:
How can I mock injected dependencies in JUnit tests? Thanks.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscribe@googlegroups.com.

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

y...@collectivehealth.com

unread,
Jun 7, 2018, 6:55:37 PM6/7/18
to google-guice
5 years late to the party. I ended up implementing my own library at my current company: https://eng.collectivehealth.com/introducing-testmodule-bdab286f12db.

David Nouls

unread,
Jun 8, 2018, 2:34:04 AM6/8/18
to google...@googlegroups.com
Never tried Jukito ? works great.

This message may contain confidential, proprietary, or protected information.  If you are not the intended recipient, you may not review, copy, or distribute this message. If you received this message in error, please notify the sender by reply email and delete this message.

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.

To post to this group, send email to google...@googlegroups.com.

y...@collectivehealth.com

unread,
Jun 8, 2018, 3:46:28 AM6/8/18
to google-guice
The problem seemed simple enough, so I didn't bother looking for an existing solution. It took less than a day and two iterations to get everything I needed into the custom library. Of course, it took a lot longer to make it look prettier. :)

David Nouls

unread,
Jun 8, 2018, 3:53:14 AM6/8/18
to google...@googlegroups.com
I had something similar implemented a few years ago. Indeed not difficult to do.

But I switched to Jukito because it offers a few advanced features:
- Interfaces and abstract classes are automatically mocked (as a singleton). In many cases you don’t need to define any binding if you use interfaces for your injected dependencies.
- you can have parameters on your test methods and these parameters are automatically injected. This can even work on @Before methods.
- Uses annotations to provide the modules to use (that would allow defining your own bindings).

Groeten,
David

y...@collectivehealth.com

unread,
Jun 8, 2018, 12:24:47 PM6/8/18
to google-guice
Thank you for the suggestion, I'll take a look at it.
Reply all
Reply to author
Forward
0 new messages