Discussion:
How do I get injected reference from Guice created instance?
'Danny S.' via google-guice
2018-02-16 09:54:00 UTC
Permalink
I just started learning Google Guice and developed a first example from Guice
git-hub <https://github.com/google/guice/wiki/Motivation>.
My code looks something like this example
<https://github.com/philipjkim/guice-example>.

My scenario to which I have a question:

In the Guice-example the BillingService calls
a CreditCardProcessor#charge(creditCard, amount).
The credit card and amount is set to the CreditCardProcessor.
My GuiceBillingService gets the CreditCardProcessor via injection and sets
the credit card to it through #chargeOrder() of my BillingService.


My test case looks like this:
@Override
public void setUp() throws Exception {
injector = Guice.createInjector(new FakeBillingModule());
}

@Override
public void tearDown() throws Exception {
injector = null;
}
public void testSuccessfulCharge() {
BillingService billingService =
injector.getInstance(BillingService.class);

Receipt receipt = billingService.chargeOrder(order, creditCard);
assertTrue(receipt.hasSuccessfulCharge());
assertEquals(16.8, receipt.getAmountOfCharge());

FakeCreditCardProcessor processor = (FakeCreditCardProcessor)
injector.getInstance(CreditCardProcessor.class);
assertEquals(creditCard, processor.getCardOfOnlyCharge());
assertEquals(16.8, processor.getAmountOfOnlyCharge());

InMemoryTransactionLog transactionLog = (InMemoryTransactionLog)
injector.getInstance(TransactionLog.class);
assertTrue(transactionLog.wasSuccessLogged());
}


Currently my module-configuration binds the CreditCardProcessor in scope
SINGLETON:

@Override
protected void configure() {
bind(TransactionLog.class).to(InMemoryTransactionLog.class).in(Scopes.SINGLETON);
bind(CreditCardProcessor.class).to(FakeCreditCardProcessor.class).in(Scopes.SINGLETON);
bind(BillingService.class).to(GuiceBillingService.class);
}


So if not SINGLETON then a new CreditCardProcessor is created and
assertEquals(creditCard, processor.getCardOfOnlyCharge()); will fail.
If my module does not configure the processor in scope SINGLETON Guice
creates a new object via injector.getInstance() - so far so good, but how
it's possible to get the same CreditCardProcessor from BillingService from
Guice instead of using getters from the BillingService-instance?

How can I get the same instance of CreditCardProcessor that has been
changed within the BillingService?

If scope SINGLETON is used the scope applies to application what means that
it's always the same instance when I run tests in parallel?

Whats the preferred way to get injected dependencies from a
Guice-created-instance? Are getters a recommended solution? I thought - if
I use a DI-framework it should be able to get all concrete instances from
it instead...?!

Thanks for some advice and help!
-Danny
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/1156b28d-bda4-43a4-b337-861b8da7a8e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stephan Classen
2018-02-16 10:33:55 UTC
Permalink
Two approaches I know of:

Use constructor injection. This means make all your dependencies final and have them passed to the constructor which is annotated with @Inject. The constructor can be package visible only. It does not need to be public.
In your test you don't need to use DI any more, simply create all dependencies in the setup and pass them to the constructor by calling the beloved "new"

Or you can use a library like jukito which will change the scope to singleton just for the tests. It also helps you to setup mocks for your tests.

I personally prefer the constructor injection. Because having to many dependencies is a code smell. And this is better visibly with constructor injection.

Be aware that with both approaches I mentioned you only test your business logic. You don't test if your guice modules configuers the injector in an appropriate way for production.
Post by 'Danny S.' via google-guice
I just started learning Google Guice and developed a first example from Guice
git-hub <https://github.com/google/guice/wiki/Motivation>.
My code looks something like this example
<https://github.com/philipjkim/guice-example>.
In the Guice-example the BillingService calls
a CreditCardProcessor#charge(creditCard, amount).
The credit card and amount is set to the CreditCardProcessor.
My GuiceBillingService gets the CreditCardProcessor via injection and sets
the credit card to it through #chargeOrder() of my BillingService.
@Override
public void setUp() throws Exception {
injector = Guice.createInjector(new FakeBillingModule());
}
@Override
public void tearDown() throws Exception {
injector = null;
}
public void testSuccessfulCharge() {
BillingService billingService =
injector.getInstance(BillingService.class);
Receipt receipt = billingService.chargeOrder(order, creditCard);
assertTrue(receipt.hasSuccessfulCharge());
assertEquals(16.8, receipt.getAmountOfCharge());
FakeCreditCardProcessor processor = (FakeCreditCardProcessor)
injector.getInstance(CreditCardProcessor.class);
assertEquals(creditCard, processor.getCardOfOnlyCharge());
assertEquals(16.8, processor.getAmountOfOnlyCharge());
InMemoryTransactionLog transactionLog = (InMemoryTransactionLog)
injector.getInstance(TransactionLog.class);
assertTrue(transactionLog.wasSuccessLogged());
}
Currently my module-configuration binds the CreditCardProcessor in scope
@Override
protected void configure() {
bind(TransactionLog.class).to(InMemoryTransactionLog.class).in(Scopes.SINGLETON);
bind(CreditCardProcessor.class).to(FakeCreditCardProcessor.class).in(Scopes.SINGLETON);
bind(BillingService.class).to(GuiceBillingService.class);
}
So if not SINGLETON then a new CreditCardProcessor is created and
assertEquals(creditCard, processor.getCardOfOnlyCharge()); will fail.
If my module does not configure the processor in scope SINGLETON Guice
creates a new object via injector.getInstance() - so far so good, but how
it's possible to get the same CreditCardProcessor from BillingService from
Guice instead of using getters from the BillingService-instance?
How can I get the same instance of CreditCardProcessor that has been
changed within the BillingService?
If scope SINGLETON is used the scope applies to application what means that
it's always the same instance when I run tests in parallel?
Whats the preferred way to get injected dependencies from a
Guice-created-instance? Are getters a recommended solution? I thought - if
I use a DI-framework it should be able to get all concrete instances from
it instead...?!
Thanks for some advice and help!
-Danny
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/1156b28d-bda4-43a4-b337-861b8da7a8e8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/D85AE355-10FF-4348-AEE6-CFD6A25C9236%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.
Loading...