Upgrading GuiceBerry from version 2 to version 3

GuiceBerry 3.0 brought many new features, such as JUnit4 support, and it is also 100% backwards compatible -- all your tests will continue to work like before.

But it does require some work to get you to be “pristinely” on GuiceBerry 3.0-land. This guide will help you navigate the changes. First, a list of the changes (and the briefest rationale):

Not all these deprecations are created equal, but they should be simple to perform -- and can be done in steps -- but do follow these steps in order to avoid problems. Transitions are always tricky -- so if you find any problem following these steps, please post to your problem the guiceberry mailing list, and I will help you:

1. GuiceBerryEnvRemapper

If you don’t use GuiceBerryEnvRemappers, skip this.

If you do, you can either create custom GuiceBerryEnvSelectors, or use the DefaultEnvSelector’s override feature to remap certain declared GuiceBerry Envs to alternative GuiceBerry Envs, much like the old Map-Based Remapper did.

2. @Inject TestCase

@Inject-ing TestCases is simply not supported anymore.

3. TestId/GuiceBerryEnvMain/TestScoped

Anywhere you import a TestId, TestScoped or GuiceBerryEnvMain, change it to import the new (non-deprecated) TestId/TestScoped/GuiceBerryEnvMain. These should are drop-in replacements

4. BasicJunit3Module

If you took care of the @Inject TestId and @Inject TestCase issues, you should be able to stop installing the BasicJunit3Module -- use the GuiceBerryModule instead. You might be doing this indirectly, by extending GuiceBerryJunit3Env. If that is the case, simply stop extending this, and instead, in your configure method, do this:

configure() {

  install(new GuiceBerryModule());

}

If your class overrides getTestScopeListener, simply change this to be a provider method. E.g. this:

  protected Class<? extends TestScopeListener> getTestScopeListener() {

    return MyTestScopeListener.class;

  }

becomes:

@Provides

TestScopeListener getTestScopeListener() {

  return new MyTestScopeListener();

}

Or go straight to TestWrappers, as defined below:

5. TestScopeListener

Change your classes that implement TestScopeListener to implement TestWrapper, and, just like in #4, the easiest thing is to make a Provider method. Note that TestWrapper does not have an equivalent to exitingScope: you should do that through a TearDownAccepter. E.g. this:

void configure() {

  bind(TestScopeListener.class).to(MyTestScopeListener.class).in(Scopes.SINGLETON);

}

class MyTestScopeListener implements TestScopeListener {

  public void enteringScope() {

    foo();

  }

  public void exitingScope() {

    bar();

  }

}

becomes:

@Provides

TestWrapper getTestWrapper(final TearDownAccepter tearDownAccepter) {

  return new TestWrapper() {

    public void toRunBeforeTest() {

      tearDownAccepter.addTearDown(new TearDown() {

        public void tearDown() throws Exception {

          bar();

        }

      });

     foo();

    }

  }

}

6. the “controllable” package

If you don’t use classes in this package, skip this.

If you do, simply change (all at once) the imports from com.google.inject.testing.guiceberry.controllable to com.google.guiceberry.controllable -- and at the same time, change your IcClients to InjectionControllers and SharedStaticVarIcStrategy to StaticMapInjectionController (drop-in replacements). Do all these steps at the same time, though!

7. GuiceBerryJunit3

If you took care of the GuiceBerryEnvRemapper you should be able to stop using GuiceBerryJunit3.setup. If you are using a TearDownTestCase, use AnnotationBasedAutoTearDownGuiceBerry, otherwise, AnnotationBasedManualTearDownGuiceBerry. Or, see item 8 below.

If you continue using the annotation-based model, you want to change all your @GuiceBerryEnvs to @AnnotatedGuiceBerryEnvs. The “AnnotationBased” classes above support both of these, but the first one is “soft deprecated”.

8. You’re done, in a way

From now on, you are now free to continue using the GuiceBerryEnv annotation (through the AnnotationBasedAutoTearDownGuiceBerry or AnnotationBasedManualTearDownGuiceBerry, whichever you chose), or you may want to use the annotationless equivalents -- AutoTearDownGuiceBerry and ManualTearDownGuiceBerry.

Good luck.

Zorzella