Class GuiceRule

  • All Implemented Interfaces:
    org.junit.rules.MethodRule

    public class GuiceRule
    extends Object
    implements org.junit.rules.MethodRule
    JUnit Rule which initializes Guice Injector for tests.

    Usage:

       public @Rule GuiceRule guice = new GuiceRule(YourGuiceModule.class);
    
       @Inject SomeClass someClass;
     
    Author:
    Michael Vorburger.ch
    • Field Detail

      • DEFAULT_STAGE

        protected static final com.google.inject.Stage DEFAULT_STAGE
        Default Stage PRODUCTION. Note that this is different from Guice's DEVELOPMENT default. We do this to avoid having to declare bindings of Listeners asEagerSingleton(), because in typical OpenDaylight projects there are Listener classes which are not @Inject, but must still be created (so that they're registered). See Guice documentation.
      • modules

        protected final Iterable<? extends com.google.inject.Module> modules
      • stage

        protected final com.google.inject.Stage stage
      • injector

        protected com.google.inject.Injector injector
    • Constructor Detail

      • GuiceRule

        public GuiceRule​(com.google.inject.Module... modules)
      • GuiceRule

        protected GuiceRule​(com.google.inject.Stage stage,
                            com.google.inject.Module... modules)
      • GuiceRule

        @SafeVarargs
        public GuiceRule​(Class<? extends com.google.inject.Module>... moduleClasses)
    • Method Detail

      • apply

        public org.junit.runners.model.Statement apply​(org.junit.runners.model.Statement base,
                                                       org.junit.runners.model.FrameworkMethod method,
                                                       Object target)
        Specified by:
        apply in interface org.junit.rules.MethodRule
      • setUpGuice

        protected void setUpGuice​(Object target)
      • tearDownGuice

        protected void tearDownGuice()