title: Injecting named Guice singleton
body: <p>I have a simple POJO:</p>
  
  <pre><code>public class MyPOJO {
      @Inject
      private Fizz fizz;
  
      private Buzz buzz;
  
      // rest of class omitted for brevity
  }
  </code></pre>
  
  <p>I would like to configure my Guice module such that there are two types of <code>Fizz</code>es that it injects:</p>
  
  <ol>
  <li>A special, globally-singleton <code>Fizz</code> instance; and</li>
  <li>Other (non-special) <code>Fizz</code> instances</li>
  </ol>
  
  <p>I want <code>MyPOJO</code> to be injected with the special/singleton instance. So I modified my code:</p>
  
  <pre><code>public class MyPOJO {
      @Inject @Named("Special-Fizz")
      private Fizz fizz;
  
      private Buzz buzz;
  
      // rest of class omitted for brevity
  }
  </code></pre>
  
  <p>Then in my module:</p>
  
  <pre><code>public class MyModule extends AbstractModule {
      @Override
      public void configure() {
          bind(Fizz.class).annotatedWith(
              Names.named("Special-Fizz"))
              .to(Fizz.class);
  
          // Other bindings here...
      }
  
      @Provides @Singleton
      private Fizz providesFizz() {
          return new Fizz(true, "Special", 12.0);
      }
  }
  </code></pre>
  
  <p>But when I try to unit test (JUnit 4.10) this:</p>
  
  <pre><code>public class MyTest {
      @Named("Special-Fizz") private Fizz specialFizz;
  
      @Test
      public void usingNamedShouldInjectSpecialFizz() {
          MyModule mod = new MyModule();
          Injector injector = Guice.createInjector(mod);
  
          specialFizz = injector.getInstance(Fizz.class);
  
          Assert.assertTrue(specialFizz != null);
      }
  }
  </code></pre>
  
  <p>This passes. So far, so good. But then if I change the name of the <code>specialFizz</code> field:</p>
  
  <pre><code>    @Named("Special-Fuzz-This-Shouldnt-Work") private Fizz specialFizz;
  </code></pre>
  
  <p>And re-run the test, <em>it still passes</em>. Why?!? Where am I going astray here? Thanks in advance.</p>