Google I/O 2009 - Big Modular Java with Guice

Google for Developers · 298K views · 2009 · 58:12

Open on YouTube

Software architecture & designTesting, debugging & developer toolstalk

Why Stack Overflow recommends it

Stack Overflow users link it as a clear introduction to dependency injection and Guice, and to back claims about why constructor injection and mock-friendly design make code easier to test.

Recommended by 9 people across 9 different months, 2009 to 2015.

What it is

A 2009 Google I/O session by Dhanji R. Prasanna and Jesse Wilson on how Google uses the Guice dependency injection framework in large Java applications. It works as an introduction to Guice, its ecosystem and the case for constructor injection and testable, type-safe design.

What people said

Strong rec Jake Wharton · 2014-02 · Stack Overflow · on “What are the specific benefits of using DI on Android?”

For example, what kind of apps would benefit the most from using DI? Dependency injection (as a pattern not a library) benefits almost all code. It promotes designing modular components which expose only the necessary APIs required to perform a specific action. When you are forced to break up pieces of your applications you have to consider how much implementation detail to expose, how the API behaves, and the visibility of classes and methods. It promotes logical abstractions of components (think: interfaces and their implementations). You certainly don't have to do this, but it ends up occurring organically anyway the more you DI things. It facilitates testability by creating a single poin

Recommends whatnick · 2009-09 · Stack Overflow · on “Other than testing, how is Dependency Injection any better than static classes/methods?”

There is a Guice video [this video] which gives a nice sample case for using D.I. If you are using a lot of 3-rd party services which need to be hooked upto dynamically D.I will be a great help.

Recommends Droo · 2009-12 · Stack Overflow · on “What are Dependency Injection & Spring Framework about?”

Here's a good video [this video] given by Bob Lee and two of his under studies about Guice (Guice is a dependency injection framework like Spring). The first 10 or so minutes are about why Guice (or dependency injection) would be better than the alternative (Factories) so it's not all just about Guice.

Recommends Michael Aaron Safyan · 2010-03 · Stack Overflow · on “Dependency Injection: What's wrong with good old-fashioned refactoring?”

That is an incorrect characterization of dependency injection. It is not that you have one implementation of a particular interface that changes over time; rather, it is possible that there will be many different implementations of an interface all at once, and which implementation will be used can vary over multiple different runs of the program. For example, in your actual program, you might want to use one implementation, while during unit testing, you might want to "mock out" that implementation with an alternative version that is easier to test. In this case, refactoring is not a solution, because you need to be able to test all the time without interrupting the rest of the development

Recommends Margus · 2011-03 · Stack Overflow · on “What is the proper way to check for and handle null return values?”

I know this is not Java question, but Java is just 1 hop away from C#, so this does not matter. To solve this problem and problems related to this, Google employees have created `Guice 2`. Here is a into video, where thy explain the problems, how thy are solved and how `Guice 2` makes your life easier: [this video]

Recommends Brian · 2011-06 · Stack Overflow · on “How to make simple android app with roboguice that injects a textView?”

I've used RoboGuice with two major projects. I'd recommend watching the following Google Guice video before diving into RoboGuice: [this video]

Recommends 0lukasz0 · 2012-06 · Stack Overflow · on “Dependency Injection - what is the difference between these two codes?”

Neither of them is proper dependency injection example. Those are rather examples of data access patterns. The first one is an example of active record pattern [LINK] . Setting orderDAO as a dependency for customer entity we could call property or setter injection. The second example could be repository pattern. [LINK] Dependency pattern here would be method injection, which translates to common invoking method with some parameters (parameters here are dependencies for method). Good way to start learning DI pattern would be reading this book [LINK] . There are also many online resources like those videos: [LINK] [LINK] [this video] I would also recommend looking for Dependency Inversion Principle

Recommends klimat · 2013-12 · Stack Overflow · on “Why field injection causes class is difficult to test?”

According Google I/O 2009 - Big Modular Java with Guice (at 29:55) [this video] using field injection makes testing harder than constructor or setter injection. Let's consider NewsService class: public class NewsService{ @Inject Storage storage; @Inject Authenticator auth; public void addNews(String data){ if(auth.authenticate()){ storage.save(data); } } } and unit test for NewsService: @RunWith(MockitoJunitRunner.class) public class NewsServiceTest{ @Mock Storage storage; @Mock Authenticator auth; @InjectMocks NewsService sut; //system under test @Test public void addNews_ShouldCallSaveOnStorage_WhenAuthIsSucceed(){ when(auth.authenticate()).thenReturn(true); sut.addNews("mocked data"); verify(s

Recommends dimo414 · 2015-03 · Stack Overflow · on “Java - How to restrict method calling from a specific method”

The right thing to do would be to revisit your requirement. A method that can only be called by certain other code paths is not compatible with public . The general best practice is to use package-private to prevent external callers, and accept that any code in the package could call the method, but won't because you or your team is auditing it. Method visibility is ultimately not a secure solution to preventing execution; someone has your .class files and the ability to execute them on a machine, they can do just about anything they want. You shouldn't spend too much time trying to lock down method calls. Instead, document the intent of the method clearly (e.g. " Helper function for methodB

Cites miku · 2009-11 · Stack Overflow · on “Strategies to block an external webservice to simulate "down" during for a testing scenario?”

Create some Mock-Webservice class or interface (and inject [LINK] it [this video] ). In there, you could test the response of your system to webservice failures and also what happens, if a web-service request take longer than expected or actually time-out. DeveloperWorks article on mock testing: [LINK]