I would like to be able to have an individual unit test configure the Principal object that Guice will inject into the component I'm testing. Currently this is done in Guice Modules and thus doesn't really allow you to change bindings on a test by test basis that I can find. Anyone know of a way?
I've got a workable solution but it's not real pretty, bind Principal.class to a TestPrincipalProvider (all exists today), TestPrincipalProvider calls out to a new singleton object to get the actual Principal. The individual tests can also get access to that singleton and configure the result they'd like to be returned. Singleton needs to be cleared in teardown.
It seems like there *must* be a way to do this on the fly with Guice but I can't find it yet. I did some digging and found mention of some introspection capabilities in Guice but nothing I tried seems to work, and I've yet to locate any examples of anyone doing this.
Thoughts?
Devan
On Fri, Apr 30, 2010 at 8:01 AM, Devan Goodwin dgoodwin@rm-rf.ca wrote:
I would like to be able to have an individual unit test configure the Principal object that Guice will inject into the component I'm testing. Currently this is done in Guice Modules and thus doesn't really allow you to change bindings on a test by test basis that I can find. Anyone know of a way?
I've got a workable solution but it's not real pretty, bind Principal.class to a TestPrincipalProvider (all exists today), TestPrincipalProvider calls out to a new singleton object to get the actual Principal. The individual tests can also get access to that singleton and configure the result they'd like to be returned. Singleton needs to be cleared in teardown.
It seems like there *must* be a way to do this on the fly with Guice but I can't find it yet. I did some digging and found mention of some introspection capabilities in Guice but nothing I tried seems to work, and I've yet to locate any examples of anyone doing this.
Thoughts?
Is it possible to get access to the injector and replace the bind with a new implementation of one that is mocked?
Pseudo code below:
MockPrincipalProvider mpp = mock(PrincipalProvider.class)... when(methodfoo).return(MyTestPrincipal); Injector inj = ETHER.getInjector(); inj.bind(Principal.class).toProvider(MockPrincipalProvider.class)
Again I'm talking out my ear here, but wondering if this is possible? Maybe you already exhausted this route.
jesus
On 10-04-30 9:01 AM, Devan Goodwin wrote:
I would like to be able to have an individual unit test configure the Principal object that Guice will inject into the component I'm testing. Currently this is done in Guice Modules and thus doesn't really allow you to change bindings on a test by test basis that I can find. Anyone know of a way?
I've got a workable solution but it's not real pretty, bind Principal.class to a TestPrincipalProvider (all exists today), TestPrincipalProvider calls out to a new singleton object to get the actual Principal. The individual tests can also get access to that singleton and configure the result they'd like to be returned. Singleton needs to be cleared in teardown.
It seems like there *must* be a way to do this on the fly with Guice but I can't find it yet. I did some digging and found mention of some introspection capabilities in Guice but nothing I tried seems to work, and I've yet to locate any examples of anyone doing this.
Thoughts?
Devan
Not sure what you're trying to test, but some thoughts:
it seems that we sometimes inject Provider<Principal>, instead of the actual object. It is probably due to scoping issues, so I would:
- modify PrincipalProvider constructor so it doesn't get HttpServletRequest injected - it's not being used. - inject Principal directly. - create test version of the class you are trying to test, extended with Principal setter (and set Principal directly in your tests). - profit?
-d
candlepin@lists.stg.fedorahosted.org