I've got a little framework (really a custom JUnit test runner and a
few support classes) for generically writing unit tests with Guice - I
didn't find anything that was out there about 8 months ago when I
started it, and was tired of writing boilerplate test setUp() methods
to initialize Guice with the right modules.
It could be open sourced - I'm wondering if anybody would find it
useful, or if that niche is already well-filled and I just missed it.
Basically you write a test like:
@RunWith (GuiceRunner.class)
@TestWith (ModuleA.class, ModuleB.class)
public void MyTest { //or, instead of @RunWith, extends GuiceTest
@Test
public void test (SomeObject arg1, SomeOtherObject arg2) {
...
}
So, the test runner just creates the injector, asks it for instances
of SomeObject and SomeOtherObject and invokes the test method.
Otherwise it's just like an ordinary JUnit test.
@TestWith can be applied to either the class or the test method, or
both if some tests need an additional module or two (no duplicates
allowed).
One other feature of possible interest - I needed to run some tests
against multiple different modules (mainly running tests against both
MySQL and Embedded H2). So you can also do
@TestWith(
value={ModuleA.class, ModuleB.class},
iterate={ModuleC.class, ModuleD.class})
in which case the test method (or class) will be invoked once using
ModuleC and again with ModuleD (the module name is munged into the
test method nae JUnit reports - which works but breaks some IDE JUnit
integration features).
And of course you can mix in regular JUnit tests in a test class and
they work the same way.
It has a couple of other features tangentially related:
@SkipWhenNetworkUnavailable - airplane coding mode
@OnInject - similar to JUnit's @Before, but after injector creation
has succeeded
@SkipWhenRunInIDE - requires passing -DargLine = ... to maven in my
IDE, but basically - skip some tests when run in an IDE, but run them
in a full build in a shell before a commit
Anyway, interesting, or just reinventing the wheel?
-Tim
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.