Categories
java spring spring-test

Testing Spring Framework 4 with Servlet 2.5

I’m having problem getting Spring Framework 4 to work with my existing project using Servlet 2.5. My web project actually runs fine, but my testcases are failing and it is caused by MockHttpServletRequest, which throws this exception:-

java.lang.NoClassDefFoundError: javax/servlet/AsyncContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)

I tried adding either dependency, but I’ll get other Servlet 3.0 related exceptions:-

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0.20090124</version>
<scope>test</scope>
</dependency>

Based on the Spring Framework website, it is said to work with Servlet 2.5. However, Spring 4’s MockHttpServletRequest seems to rely on Servlet 3.0 forward.

How do I fix this problem? Thanks.

Restrict the dependencies for spring-test to a version prior to 4, like spring-test-3.2.

I was not aware that Spring-4 dropped support to Servlet-2.5. 3.9 Testing Improvements says:

As of Spring 4.0, the set of mocks in the org.springframework.mock.web package is now compatible with Servlet 3.0.

I don’t understand that “compatible with servlet-3” means a dropped servlet-2.5 support. If it was intentionally it should at least go into the reference documentation. So it might even be worth filing a bug (SPR-11292) about it.

But wait there is 3.4 Java EE 6 and 7:

Java EE version 6 or above is now considered the baseline for Spring Framework 4, with the JPA 2.0 and Servlet 3.0 specifications being of particular relevance.[..] it is possible to deploy a Spring application into a Servlet 2.5 environment; however, Servlet 3.0+ is recommended when at all possible.

So I think that qualifies as mentioned in the documentation.


Update:
The Spring 4.0.1 reference documentation is now more clear about the Mocks:

Servlet 3.0+ is strongly recommended and a prerequisite in Spring’s test and mock packages for test setups in development environments.