Google Groups Home
Help | Sign in
Field injection and testability
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Eric M. Burke  
View profile
 More options Jul 22, 9:24 am
From: "Eric M. Burke" <burke.e...@gmail.com>
Date: Tue, 22 Jul 2008 06:24:55 -0700 (PDT)
Local: Tues, Jul 22 2008 9:24 am
Subject: Field injection and testability
Guice Best Practices (http://code.google.com/p/google-guice/wiki/
GuiceBestPractices) has this to say about field injection:

"Your class is not testable!"

Someone sent me a question via email and I figured I'd pass it along
to this group. Here is my friend's question:

"There's a page that summarizes Guice Best Practices. It compares
field, method and constructor injection. One of the stated cons of
field injection says "Your class is not testable!". Why is that? Can't
you inject fields with different values for tests by writing a
different Module class that is used by tests?"

So when the best practices page says 'not testable', is it more
accurate to say 'hard to test without using a Guice module in your
tests' ?  I understand that with constructor or method injection, I
don't even need Guice in my tests. I could just create mock POJOs and
call setter methods myself. But with field injection, I'd need to
either use a module with Guice, or else resort to reflection tricks to
set the fields.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robbie Vanbrabant  
View profile
 More options Jul 22, 9:53 am
From: "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
Date: Tue, 22 Jul 2008 15:53:36 +0200
Local: Tues, Jul 22 2008 9:53 am
Subject: Re: Field injection and testability

What you're saying is accurate. Also note that with field injection you
enter a gray zone when it comes to thread safety and safe publication, since
you can't make your fields final and Guice doesn't inject the values until
after the constructor executes. In a sense, field injection is the
"autowiring" of injection: simple to begin with but surprisingly difficult
to get right in the long term.

But it works great for examples. :-)

Robbie

On Tue, Jul 22, 2008 at 3:24 PM, Eric M. Burke <burke.e...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Lee  
View profile
 More options Jul 22, 10:20 am
From: "Bob Lee" <crazy...@crazybob.org>
Date: Tue, 22 Jul 2008 07:20:12 -0700
Local: Tues, Jul 22 2008 10:20 am
Subject: Re: Field injection and testability

On Tue, Jul 22, 2008 at 6:24 AM, Eric M. Burke <burke.e...@gmail.com> wrote:

> So when the best practices page says 'not testable', is it more
> accurate to say 'hard to test without using a Guice module in your
> tests' ?  I understand that with constructor or method injection, I
> don't even need Guice in my tests. I could just create mock POJOs and
> call setter methods myself. But with field injection, I'd need to
> either use a module with Guice, or else resort to reflection tricks to
> set the fields.

That's correct. We should update that doc--"hard to test by hand." I
actually kind of like field injection. It's concise. You don't have to worry
about circular references. You don't have to worry about user code executing
during injection (with the exception of providers). Of course, you can't
access those fields from your constructor... that kind of sucks.

On Tue, Jul 22, 2008 at 6:53 AM, Robbie Vanbrabant <

robbie.vanbrab...@gmail.com> wrote:
> What you're saying is accurate. Also note that with field injection you
> enter a gray zone when it comes to thread safety and safe publication, since
> you can't make your fields final and Guice doesn't inject the values until
> after the constructor executes. In a sense, field injection is the
> "autowiring" of injection: simple to begin with but surprisingly difficult
> to get right in the long term.

I don't think there are any thread safety concerns here. Injection happens
all in one thread. You can make the fields final. Of course, you'll have to
initialize them to some default value, but Guice will overwrite that value
and you'll get the same semantics that you get if the field's set in the
constructor (
http://jeremymanson.blogspot.com/2008/07/immutability-in-java-part-3....).

Bob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robbie Vanbrabant  
View profile
 More options Jul 22, 11:07 am
From: "Robbie Vanbrabant" <robbie.vanbrab...@gmail.com>
Date: Tue, 22 Jul 2008 17:07:07 +0200
Local: Tues, Jul 22 2008 11:07 am
Subject: Re: Field injection and testability

Thanks for the link, that's something I didn't know. Which somehow confirms
my concerns, though.

Robbie


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Lee  
View profile
 More options Jul 22, 11:09 am
From: "Bob Lee" <crazy...@crazybob.org>
Date: Tue, 22 Jul 2008 08:09:49 -0700
Local: Tues, Jul 22 2008 11:09 am
Subject: Re: Field injection and testability

On Tue, Jul 22, 2008 at 8:07 AM, Robbie Vanbrabant <

robbie.vanbrab...@gmail.com> wrote:
> Thanks for the link, that's something I didn't know. Which somehow confirms
> my concerns, though.

I actually shouldn't have mentioned the semantics of final. The important
thing is that all injection happens in a single thread, so there are no
concurrency concerns w/ field vs. constructor injection.

Bob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sam Berlin  
View profile
 More options Jul 22, 11:23 am
From: "Sam Berlin" <sber...@gmail.com>
Date: Tue, 22 Jul 2008 11:23:20 -0400
Local: Tues, Jul 22 2008 11:23 am
Subject: Re: Field injection and testability

> I don't think there are any thread safety concerns here. Injection happens
> all in one thread. You can make the fields final. Of course, you'll have to
> initialize them to some default value, but Guice will overwrite that value
> and you'll get the same semantics that you get if the field's set in the
> constructor
> (http://jeremymanson.blogspot.com/2008/07/immutability-in-java-part-3....).

Wow, is this really true?  It goes against everything I thought I'd
known with threading concerns & publishing.  I also had no clue that
'setAccessible' let you overwrite final fields -and- did it safely
with threading.

Sam


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bob Lee  
View profile
 More options Jul 22, 11:25 am
From: "Bob Lee" <crazy...@crazybob.org>
Date: Tue, 22 Jul 2008 08:25:18 -0700
Local: Tues, Jul 22 2008 11:25 am
Subject: Re: Field injection and testability

On Tue, Jul 22, 2008 at 8:23 AM, Sam Berlin <sber...@gmail.com> wrote:
> Wow, is this really true?

As of Java 1.5, yes. Before that, it wasn't really specified.

Bob


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google