Package ratpack.guice

Class BindingsImposition

  • All Implemented Interfaces:
    Imposition

    public final class BindingsImposition
    extends Object
    implements Imposition
    An imposition of additional and overridden Guice bindings, used by Guice.registry(Action) and similar methods.

    This is typically used to override bindings at test time.

    
     import ratpack.core.server.RatpackServer;
     import ratpack.guice.BindingsImposition;
     import ratpack.guice.Guice;
     import ratpack.core.impose.ImpositionsSpec;
     import ratpack.test.MainClassApplicationUnderTest;
    
     import static org.junit.jupiter.api.Assertions.assertEquals;
    
     public class Example {
    
       public static class App {
         public static void main(String[] args) throws Exception {
           RatpackServer.start(s -> s
             .registry(Guice.registry(r ->
               r.bindInstance(String.class, "foo")
             ))
             .handlers(c -> c
               .get(ctx -> ctx.render(ctx.get(String.class)))
             )
           );
         }
       }
    
       public static void main(String[] args) throws Exception {
         new MainClassApplicationUnderTest(App.class) {
           @Override
           protected void addImpositions(ImpositionsSpec impositions) {
             impositions.add(BindingsImposition.of(r ->
               r.bindInstance(String.class, "bar")
             ));
           }
         }.test(testHttpClient ->
           assertEquals("bar", testHttpClient.getText())
         );
       }
     }
     
    Since:
    1.2
    See Also:
    Impositions
    • Method Detail

      • getBindings

        public Action<? super BindingsSpec> getBindings()
        The bindings to add/override.
        Returns:
        the bindings to add/override
      • of

        public static BindingsImposition of​(Action<? super BindingsSpec> bindings)
        Creates a new bindings imposition of the given bindings.
        Parameters:
        bindings - the bindings to impose
        Returns:
        a new bindings imposition