Class GuiceyHooksRule

java.lang.Object
org.junit.rules.ExternalResource
ru.vyarus.dropwizard.guice.test.GuiceyHooksRule
All Implemented Interfaces:
org.junit.rules.TestRule

public class GuiceyHooksRule extends org.junit.rules.ExternalResource
Junit rule for changing application configuration (remove some components or register test specific (e.g. mocks)). Supposed to be used in conjunction with DropwizardAppRule or GuiceyAppRule. Must be used ONLY with RuleChain because normally rules order is not predictable:
static GuiceyAppRule RULE = new GuiceyAppRule(App.class, null);
    {@literal @}ClassRule
    public static RuleChain chain = RuleChain
            .outerRule(new GuiceyHooksRule((builder) -> builder.modules(...)))
            .around(RULE);
 
To declare common extensions for all tests, declare common rule in test class (without @ClassRule annotation!) and use it in chain:
public class BaseTest {
         static GuiceyHooksRule BASE = new GuiceyHooksRule((builder) -> builder.modules(...))
     }

     public class SomeTest extends BaseTest {
         static GuiceyAppRule RULE = new GuiceyAppRule(App.class, null);
         {@literal @}ClassRule
         public static RuleChain chain = RuleChain
            .outerRule(BASE)
            .around(new GuiceyHooksRule((builder) -> builder.modules(...)) // optional test-specific staff
            .around(RULE);
     }
 

IMPORTANT: rule will not work with spock extensions (because of lifecycle specifics)! Use UseGuiceyHooks or new hooks attribute in ru.vyarus.dropwizard.guice.test.spock.UseGuiceyApp or ru.vyarus.dropwizard.guice.test.spock.UseDropwizardApp instead.

Rule is thread safe: it is assumed that rule will be applied at the same thread as test application initialization.

Since:
11.04.2018
  • Constructor Details

    • GuiceyHooksRule

      public GuiceyHooksRule(ru.vyarus.dropwizard.guice.hook.GuiceyConfigurationHook... hooks)
  • Method Details

    • before

      protected void before() throws Throwable
      Overrides:
      before in class org.junit.rules.ExternalResource
      Throws:
      Throwable
    • after

      protected void after()
      Overrides:
      after in class org.junit.rules.ExternalResource