Hi Bill!
nope, unfortunately it doesn't work, due to the know limitation
exposed in my previous message :(
If you are interested on logging actions just after the object has
been created and injected, one work-around to that problem could be
registering your own TypeListener:
class MyOwnLoggingModule
extends AbstractModule
{
@Override
protected void configure()
{
bindListener( Matchers.any(), new TypeListener()
{
@Override
<I> void hear( TypeLiteral<I> type, TypeEncounter<I> encounter )
{
encounter.register( new InjectionListener<I>()
{
public void afterInjection( I injectee )
{
( (MyType) injectee ).invokeLoggingMethod();
}
} );
}
}
}
}
We are preparing a lifecycle simplification module for that purpose,
but it is not ready yet...
HTH, all the best!
-Simo
[1]
http://google-guice.googlecode.com/git/javadoc/com/google/inject/Binder.html#bindListener(com.google.inject.matcher.Matcher<?
super com.google.inject.TypeLiteral<?>>,
com.google.inject.spi.TypeListener)
http://people.apache.org/~simonetripodi/
http://twitter.com/simonetripodi
On Sun, Jun 16, 2013 at 4:22 PM, William Speirs <[email protected]> wrote:
> I tried changing the Logger to being a static field as I'm not sure how
> Onami would assign that field before the object was constructed (and I'm
> using the logger in the constructor), but that didn't work.
>
> Any ideas?
>
> Bill-
>
>
> On Sat, Jun 15, 2013 at 4:55 PM, William Speirs <[email protected]> wrote:
>>
>> I'm trying to use Slf4jLoggingModule in a unit test with an
>> AssistedInject. Here is my basic setup:
>>
>>
>> public class BaseClass {
>> private @InjectLogger Logger LOG;
>>
>> public BaseClass() {
>> LOG.debug("Calling constructor"); // dies with NPE
>> }
>>
>> public class ClassA extends BaseClass {
>> private @InjectLogger Logger LOG;
>>
>> @Inject
>> public ClassA(@Assisted Bean bean) {
>> super();
>> }
>>
>> public interface ClassAFactory {
>> public ClassA create(Bean bean);
>> }
>> }
>>
>> @RunWith( OnamiRunner.class )
>> @MockFramework( MockType.MOCKITO )
>> public class ClassATest extends AbstractModule {
>>
>> @Inject ClassAFactory factory;
>>
>> @GuiceProvidedModules
>> public static Module createComplexModule() {
>> return new Slf4jLoggingModule();
>> }
>>
>> @Override
>> public void configure() {
>> install(new FactoryModuleBuilder()
>> .implement(ClassA.class, ClassA.class)
>> .build(ClassAFactory.class));
>> }
>>
>> @Test
>> public void test() throws Exception {
>> ClassA a = factory.create(bean);
>> }
>> }
>>
>>
>> What am I doing wrong? This is my first time using Onami and so I'm
>> sure I'm missing something obvious/stupid.
>>
>> Thanks...
>>
>> Bill-
>
>