Getting test description data in GuiceBerryModule.configure

50 views
Skip to first unread message

cgbateman72

unread,
Jan 24, 2011, 7:45:54 PM1/24/11
to guiceberry-users
I have a test case something like:

@Inject
private Model _testObject;

@Test
public void test1() { /* do some stuff to _testObject */ }
@Test
public void test2() { }
@Test
public void test3() { ... }


I would like to load a different copy of _testObject to inject
depending on what test is being run (i.e. test1 vs. test2). Ideally
it would be nice if I could specify the name of the file to load from
in an annotation on each test like:

@Test
@PathToModel(path = "/testdata/dataFileForTest1.dat")
public void test1() ....

So far, I haven't been able to find a way to do this because from
GuiceBerryModule.configure override, I don't have the current test
context. Nor can I pass the context from a selector, since the
selector only allows me to provide a class, not a configured instance.


Thanks,

Cameron

Luiz-Otavio Zorzella

unread,
Jan 26, 2011, 12:02:27 PM1/26/11
to guiceber...@googlegroups.com
It's VERY simple for you do create your own custom JUnit4 adapter
bridge to GuiceBerry, and that should be enough for you. Here:

1. Copy and paste com.google.guiceberry.junit4.GuiceBerryRule source
code to your own "MyGuiceBerryRule"

2. in the apply method, store "FrameworkMethod method" (and maybe you
need "Object target" as well) in a thread local (or just a plain
static, if you don't know how to do thread locals, or don't feel like
learning)

3. create an @TestScoped binding from FrameworkMethod to the thread
local (or static) you created in #2

4. change your test to use your MyGuiceBerryRule instead of the one
shipped with GuiceBerry

5. Create and bind a TestWrapper to your GuiceBerry env, have
FrameworkMethod injected into it. Have it read the frameworkmethod's
annotations to look for @PathToModel and populate your Model with the
data from that path.

Peace,

Z

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

cgbateman72

unread,
Jan 31, 2011, 6:12:57 PM1/31/11
to guiceberry-users
Thanks,

When you say "create an @TestScoped binding from FrameworkMethod to
the thread local (or static) you created in #2", where do I create
that binding? Also, how do I create a TestWrapper without losing what
is in TestCaseScaffolding?


Thanks,

Cameron

On Jan 26, 9:02 am, Luiz-Otavio Zorzella <zorze...@gmail.com> wrote:
> It's VERY simple for you do create your own custom JUnit4 adapter
> bridge to GuiceBerry, and that should be enough for you. Here:
>
> 1. Copy and paste com.google.guiceberry.junit4.GuiceBerryRule source
> code to your own "MyGuiceBerryRule"
>
> 2. in the apply method, store "FrameworkMethod method" (and maybe you
> need "Object target" as well) in a thread local (or just a plain
> static, if you don't know how to do thread locals, or don't feel like
> learning)
>
> 3. create an @TestScoped binding from FrameworkMethod to the thread
> local (or static) you created in #2
>
> 4. change your test to use your MyGuiceBerryRule instead of the one
> shipped with GuiceBerry
>
> 5. Create and bind a TestWrapper to your GuiceBerry env, have
> FrameworkMethod injected into it. Have it read the frameworkmethod's
> annotations to look for @PathToModel and populate your Model with the
> data from that path.
>
> Peace,
>
> Z
>

Luiz-Otavio Zorzella

unread,
Jan 31, 2011, 6:31:12 PM1/31/11
to guiceber...@googlegroups.com
On Mon, Jan 31, 2011 at 3:12 PM, cgbateman72 <cgbat...@gmail.com> wrote:
> Thanks,
>
> When you say  "create an @TestScoped binding from FrameworkMethod to
> the thread local (or static) you created in #2", where do I create
> that binding?

Create that binding in your guiceberry env (or any module installed by it).

> Also, how do I create a TestWrapper without losing what
> is in TestCaseScaffolding?

I don't know how TestCaseScaffolding came into the conversation, it
has nothing to do with TestWrapper. See the tutorial, there's one that
covers TestWrappers. What you will need is something like this in your
GuiceBerry env:

@Provides
@TestScoped
TestWrapper testWrapper(final FrameworkMethod currentlyRunningTest) {
return new TestWrapper() {
public void toRunBeforeTest() {
// read annotations and stuff from currentlyRunningTest, and
populate the test object here.

Reply all
Reply to author
Forward
0 new messages