Testing Lazy?

393 views
Skip to first unread message

Jesse Wilson

unread,
Nov 17, 2012, 10:42:34 AM11/17/12
to dagger-...@googlegroups.com

Guice's Providers.of() makes it easy to unit test Provider users. Doing the same with Dagger requires a lot of anonymous inner class boilerplate:

  @Test public void grinding() {
    final Grinder grinder = mock(Grinder.class);
    Lazy<Grinder> lazyGrinder = new Lazy<Grinder>() {
      public Grinder get() {
        return grinder;
      }
    }
    GridingCoffeeMaker coffeeMaker = new GridingCoffeeMaker(grinder);
    ...
  }

Should we add an API like Dagger.lazy() ? I'm tempted to just wait for JDK 8's Project Lambda, which eliminates the boilerplate directly.

Christian Gruber

unread,
Nov 18, 2012, 3:35:19 PM11/18/12
to dagger-...@googlegroups.com
> 8's Project Lambda <http://openjdk.java.net/projects/lambda/>, which
> eliminates the boilerplate directly.

You're really talking about a LazyImpl.of(foo) which is already eager at
construction, but typed as the lazy wrapper for testing.

Yeah. I'm tempted to do it to make testing easier with a big fat note
about how one might do it once Lambda's are in practice. I think we're
a long long way before most people will practically be able to use
lambdas in production code. It's not just waiting on Sun/Oracle, it's
waiting on everyone to adopt 8 inside corporate technology stacks.

Though since there are potentially nice testing utilities we could do in
the short-term, maybe there's should be a test lib project that we could
collect them in, so these things don't leak into production code. Don't
know if we have such a list yet, mind you.
Reply all
Reply to author
Forward
0 new messages