Testing injected HttpClient in junit test

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

Testing injected HttpClient in junit test

mixmasta
I have a standalone class with HttpClient as constructor (injected). I want unit test the class - how do I go about passing in an http client at test time.


private final HttpClient httpClient;

    @Inject
    public StormpathProvider(HttpClient httpClient) {
        this.httpClient = httpClient;
    }

Is there a helper class to setup the Ratpack environment. Basically need a registry and httpclient.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Testing injected HttpClient in junit test

mixmasta
I extended an abstract module and used bound the DefaultHttpClient instance to the HttpClient.

Not sure if that is the best practice when unit testing.

Reply | Threaded
Open this post in threaded view
|

Re: Testing injected HttpClient in junit test

danhyun
ApplicationUnderTest is a test fixture http://ratpack.io/manual/current/api/ratpack/test/ApplicationUnderTest.html that stands up a full Ratpack application.

In the example-books project there is an example of using ApplicationUnderTest to standup the example-books Ratpack application and to modify the registry

https://github.com/ratpack/example-books/blob/master/src/test/groovy/ratpack/examples/book/fixture/ExampleBooksApplicationUnderTest.groovy

You can use the example to provide your own HttpClient for your testing purposes.