Revision: 1048
Author: limpbizkit
Date: Thu Jul 23 10:46:52 2009
Log: a test for what happens when creating a singleton fails
http://code.google.com/p/google-guice/source/detail?r=1048

Modified:
  /trunk/test/com/google/inject/ScopesTest.java

=======================================
--- /trunk/test/com/google/inject/ScopesTest.java       Mon Jun 22 20:16:48 2009
+++ /trunk/test/com/google/inject/ScopesTest.java       Thu Jul 23 10:46:52 2009
@@ -63,6 +63,7 @@
      NotASingleton.nextInstanceId = 0;
      Implementation.nextInstanceId = 0;
      ProvidedBySingleton.nextInstanceId = 0;
+    ThrowingSingleton.nextInstanceId = 0;
    }

    public void testSingletons() {
@@ -546,4 +547,31 @@
      }
      return builder.build();
    }
-}
+
+  @Singleton
+  static class ThrowingSingleton {
+    static int nextInstanceId;
+    final int instanceId = nextInstanceId++;
+
+    ThrowingSingleton() {
+      if (instanceId == 0) {
+        throw new RuntimeException();
+      }
+    }
+  }
+
+  public void testSingletonConstructorThrows() {
+    Injector injector = Guice.createInjector();
+
+    try {
+      injector.getInstance(ThrowingSingleton.class);
+      fail();
+    } catch (ProvisionException expected) {
+    }
+
+    // this behaviour is unspecified. If we change Guice to re-throw the  
exception, this test
+    // should be changed
+    injector.getInstance(ThrowingSingleton.class);
+    assertEquals(2, ThrowingSingleton.nextInstanceId);
+  }
+}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to