Ratpack testing

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Ratpack testing

Bertrand Quenin
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack testing

danhyun
Hi Bertrand,

A few clarifications here, Impositions allow you to add to the registry rather than replace entries in the registry. The registry effectively acts as a Map with LIFO semantics, that is for a given key, the latest entry wins.

You are correct in your observation that Imposition seems to merge the test registry with your main application registry.

Having said that, the Guice utility shipped with Ratpack allows you to register the contents of a Guice module with the Ratpack registry via BindingsSpec#module from within the Guice.registry() call. There's no way to "replace" a module.

In this situation what I would recommend is providing a different set of configuration properties that defines a Datasource so that you only provide overrides for properties and not for actual entries.

An example can be found as follows:

import ratpack.impose.Impositions;
import ratpack.impose.ServerConfigImposition;
import ratpack.server.ServerConfig;
import ratpack.test.embed.EmbeddedApp;

import static groovy.util.GroovyTestCase.assertEquals;
import static java.util.Collections.singletonMap;

public class Example {
  public static void main(String[] args) throws Exception {
    Impositions.of(i ->
      i.add(ServerConfigImposition.of(s -> s.props(singletonMap("foo", "imposed!"))))
    ).impose(() ->
      EmbeddedApp.of(s -> s
        .serverConfig(c -> c
          .props(singletonMap("foo", "original"))
        )
        .handlers(c -> c
          .get(ctx ->
            ctx.render(ctx.get(ServerConfig.class).get("/foo", String.class))
          )
        )
      )
    ).test(testHttpClient ->
      assertEquals("imposed!", testHttpClient.getText())
    );
  }
}


In this case you only have a single instance of ServerConfig object in the registry.


To address the second point I would guess that because our remote control's exec is specifically for Groovy (because we make use of  Closures within remote control and Closure delegation) , you would not be able to simply call "get".

Unless this closure is Groovy code, Groovy, via remote control will have no idea how to resolve method delegation here

remote.exec {
      val userRepository = get(UserRepository) // Error:The method get(Class<UserRepository>) is undefined
      userRegistry.drop() // This is an example
}

You'll need to find another way to get at the registered components if you don't want to use Groovy. You might try imposing an object into the registry that can grab the desired component for you, this way you can provide your interceptor outside of the registry.
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack testing

Bertrand Quenin
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Ratpack testing

danhyun
Great, also feel free to register for the Ratpack Slack chat http://slack-signup.ratpack.io/

You can ask questions in real time and also benefit from seeing others ask questions.