Simone- Following up on this with a new idea. If I setup my logger in my class as static, is there some way to ONLY inject that static field? Then -- and I'm not 100% sure how Java works here -- simply call new and the classpath loader would return an object that has the static field assigned?
Basically, I'm looking for a way to NOT use the injector at all during unit tests, but simply inject the static field in my object. Then I was looking at Onami's AutoBind (http://onami.apache.org/autobind/index.html) to walk through my classes and autobind all of my static fields annotated with @InjectLogger. Does that even make sense? Thoughts? Thanks... Bill- On Mon, Jun 17, 2013 at 6:53 AM, William Speirs <[email protected]> wrote: > No rush, and I appreciate it... thanks! > > Bill- > > On Jun 17, 2013 4:02 AM, "Simone Tripodi" <[email protected]> wrote: >> >> Hi again Bill, >> >> I can prepare you a workaround - I am now packing to leave for >> Switzerland, I am posting an update tonight. >> >> Best, >> -Simo >> >> http://people.apache.org/~simonetripodi/ >> http://twitter.com/simonetripodi >> >> >> On Mon, Jun 17, 2013 at 12:57 AM, William Speirs <[email protected]> >> wrote: >> > Because I have value parameters that I pass in as well (port number) >> > that >> > I'd like to print to info. >> > >> > Bill- >> > >> > On Jun 16, 2013 4:35 PM, "Simone Tripodi" <[email protected]> >> > wrote: >> >> >> >> Hi Bill, >> >> >> >> my silly question is: why do you need invoking the logger in the >> >> constructor? :) >> >> >> >> TIA, >> >> -Simo >> >> >> >> http://people.apache.org/~simonetripodi/ >> >> http://twitter.com/simonetripodi >> >> >> >> >> >> On Sun, Jun 16, 2013 at 6:06 PM, William Speirs <[email protected]> >> >> wrote: >> >> > OK... I'll wait for that new module and maybe give Onami another >> >> > shot. >> >> > >> >> > For what it's worth, it seems like a lot more code in my unit tests >> >> > when >> >> > trying to use Onami. IMO, you never want to use the injector when >> >> > running a >> >> > unit test. It's best to inject via constructor, then simply call new >> >> > in >> >> > your >> >> > unit test and provide mocked instances via the constructor. Using >> >> > @LoggerInject violates this because it's a field injection, and >> >> > therefore is >> >> > causing the issue. >> >> > >> >> > So it's a question of "is it worth saving typing to setup the logger >> >> > in >> >> > each >> >> > class" vs "the headache of setting up the injector in each unit >> >> > tests"? >> >> > Right now, I'm not sure... >> >> > >> >> > Thanks... >> >> > >> >> > Bill- >> >> > >> >> > >> >> > On Sun, Jun 16, 2013 at 11:47 AM, Simone Tripodi >> >> > <[email protected]> >> >> > wrote: >> >> >> >> >> >> Hi Bill, >> >> >> >> >> >> I am sorry you stumbled in a Guice/Onami-Logger limitation: >> >> >> Onami-Logger indeed injects the logger instance AFTER the injectee >> >> >> is >> >> >> created by the Guice framework, via a MembersInjector<I> >> >> >> implementation, that is why you cannot use the logged inside the >> >> >> constructor - it has not been injected yet :( >> >> >> >> >> >> HTH, all the best, >> >> >> -Simo >> >> >> >> >> >> http://people.apache.org/~simonetripodi/ >> >> >> http://twitter.com/simonetripodi >> >> >> >> >> >> >> >> >> On Sat, Jun 15, 2013 at 10: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- >> >> > >> >> >
