java.lang.Object
org.opendaylight.infrautils.inject.guice.testutils.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 Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final com.google.inject.Stage
    Default Stage PRODUCTION.
    protected com.google.inject.Injector
     
    protected final Iterable<? extends com.google.inject.Module>
     
    protected final com.google.inject.Stage
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
    GuiceRule(com.google.inject.Module... modules)
     
    protected
    GuiceRule(com.google.inject.Stage stage, com.google.inject.Module... modules)
     
     
    GuiceRule(Class<? extends com.google.inject.Module>... moduleClasses)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    org.junit.runners.model.Statement
    apply(org.junit.runners.model.Statement base, org.junit.runners.model.FrameworkMethod method, Object target)
     
    protected void
     
    protected void
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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 Details

    • 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 Details

    • 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()