Guice and JUnit proposal

431 views
Skip to first unread message

Antoine DESSAIGNE

unread,
Feb 3, 2011, 12:52:18 PM2/3/11
to Guice
Hi everyone,

I've created 2 small classes that really help me writing JUnit tests with Guice. I wanted to share them with you, you'll find them attached. See below for a sample use.

First you have to create a class that implements the Module interface (that should already be done). Its only requirement is to have a no-arg constructor.
public class DummyModule extends AbstractModule {
@Override
protected void configure() {
bind(String.class).toInstance("TEST");
}
}

Then you write your test:
@RunWith(GuiceTestRunner.class)
@WithModules({DummyModule.class})
public class DummyTest {
private final String value;
@Inject
public DummyTest(String value) {
this.value = value;
}
@Test
public void test() {
Assert.assertEquals("TEST", this.value);
}
}

And voilà.

Hope this helps,

Antoine.
GuiceTestRunner.java
WithModules.java

Christian Goudreau

unread,
Feb 3, 2011, 1:06:24 PM2/3/11
to google...@googlegroups.com
Nice work !

There's also Jukito that simplify test like you did. There's also an automocking feature using Mockito and well, I can't live without it now that I use it !

If you already know Guice berry, Jukito is a lot simplier.

Cheers,

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.



--
Christian Goudreau

Antoine DESSAIGNE

unread,
Feb 7, 2011, 3:25:56 AM2/7/11
to Guice
Hi,

When I posted this small code, I didn't thought about the licensing. Since it has been asked, I release this code into the public domain. You can do whatever you whishes to do with this code but (as usual) at your own risk (no garanties what so ever). There is no need to mention my name, i'm just glad this code helps.

Antoine.


2011/2/5 Russ Milliken <russ.m...@gmail.com>
Hi Antoine,

What you have shared is exactly what I need.  Thank you!  Do you plan on formally releasing this into the public domain under some sort of license?  I don't know if you plan on adding more features or making patches in the future.

Regards,

-Russ

On Thu, Feb 3, 2011 at 12:52 PM, Antoine DESSAIGNE <antoine....@gmail.com> wrote:
--

Jason van Zyl

unread,
Feb 7, 2011, 10:27:56 AM2/7/11
to google...@googlegroups.com
On Feb 7, 2011, at 3:25 AM, Antoine DESSAIGNE wrote:

Hi,

When I posted this small code, I didn't thought about the licensing. Since it has been asked, I release this code into the public domain. You can do whatever you whishes to do with this code but (as usual) at your own risk (no garanties what so ever). There is no need to mention my name, i'm just glad this code helps.


If you want minimal fuss with licensing then use the MIT license. Releasing code into the public domain still leaves you at risk. The MIT license is really as close as you can get to public domain while still giving yourself the necessary legal protection i.e. use at your own risk , no guarantees are made, if something terribly does go wrong through the use of the software then it's the user's problem, not yours. You also need to put this license in the headers of the sources to be valid and enforceable. Take the 10 minutes to toss in an MIT license file and change the headers, there's no downside.

Thanks,

Jason

----------------------------------------------------------
Jason van Zyl
Founder,  Apache Maven
http://twitter.com/jvanzyl
---------------------------------------------------------

First, the taking in of scattered particulars under one Idea,
so that everyone understands what is being talked about ... Second,
the separation of the Idea into parts, by dividing it at the joints,
as nature directs, not breaking any limb in half as a bad carver might.

  -- Plato, Phaedrus (Notes on the Synthesis of Form by C. Alexander)



andineck

unread,
Nov 4, 2011, 5:12:26 PM11/4/11
to google...@googlegroups.com
Hi Antoine

Thank you very much for your code. I am using it in my integration tests. I ran into the problem, that It did only work with the test class annotated with @WithModules.
Because I wanted to be able to place it also in the subclasses or interfaces of the test class, I modified your code to allow that. Although I am not sure wether it makes sense in the interface...

Here is the modified code....


GuiceTestRunner.java
WithModules.java

David Sowerby

unread,
Nov 5, 2011, 1:33:33 PM11/5/11
to google-guice
HI Antoine

Thanks for making the code public. You may also want to look at
Mycila, which we have been using for a little while and found to be a
good Junit / Guice solution.

http://code.google.com/p/mycila/#Mycila_Guice
>  GuiceTestRunner.java
> 3KViewDownload
>
>  WithModules.java
> < 1KViewDownload
Reply all
Reply to author
Forward
0 new messages