2

Background: I'm developing servlets using guice with guice-servlet frameworks. I'd like to test my servlets by:

  1. Servlet unit tests: isolating servlets from other dependencies and simulating client, by providing get/post requests
  2. Servlet integration tests: simulating clients, but without servlet isolation. Also, as part of integration tests, I'd like to test my integration with Guice itself.

Question: I'm looking for servlet testing framework which can allow me to perform both kinds of tests described above.

Meanwhile I took a look at HttpUnit, but I didn't find a way to connect Guice to it: servlets can be registered in ServletRunner, but they are provided by class name. This means that ServletRunner will instantiate them (without injecting dependencies).

Also I found tadedon project's GuiceServletMockModule. Seems like exactly what I'm looking for. But I'm not sure it is still maintained. For example, I was not able to fetch their maven package from their maven repository.

Thanks

Denis Itskovich
  • 4,383
  • 3
  • 32
  • 53

1 Answers1

3

In order to unit test your servlet (instance of HttpServlet), you do not need any framework - you can just create new instance of the servlet class, and directly call desired method while passing mocked request and response objects.

If you need to isolate your code from Guice dependencies, you can create instance of your class using Injector configured with mocks (in @Before method), for example:

Injector injector = Guice.createInjector( new AbstractModule() {
    @Override
    protected void configure() {
        bind( MyClass.class ).toInstance( mock(MyClass.class) );
        bind( AnotherClass.class ).toInstance( mock(AnotherClass.class) );
    }
} );
//Create new instance of class  that needs injections
ThirdClass thirdInstance = injector.getInstance( ThirdClass.class );

As for the integration tests, you can try to use SoapUI - it works well with SOAP and REST applications, but you can adapt it to generic servlets tests.

Dmitry
  • 1,056
  • 1
  • 8
  • 17
  • My problem was not with the isolation. The reason I was looking for a testing framework, is that it allows to mock HttpServletRequest/HttpServletResponse, so they can be used easily in the test. HttpUnit for example, allows to mock them, but as I understood, the mocks are not exposed for stand-alone usage. Instead, you can simulate client using HttpUnit. But in this case, the servlet instance will be created by the HttpUnit itself (by class name, using reflection) so dependencies will remain not injected – Denis Itskovich Jan 05 '15 at 04:07
  • As for integration testing, my intention was actually to perform partial integration - before actual deployment. I wanted to run integration test also under `junit`. Just in case of integration test, I wanted to check the whole servlet, without dependency isolation – Denis Itskovich Jan 05 '15 at 04:11
  • You can use Mockito framework to mock any objects, in particular HttpRequest and HttpResponse: `HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class);` Integration tests require the servlet up and running, i.e. actually deployed to servlet container (i.e. Tomcat, or GAE). – Dmitry Jan 05 '15 at 13:28