[SPR-11049] Cannot create MockHttpSession with servlet 2.x Created: 31/Oct/13  Updated: 15/Jan/19  Resolved: 13/Dec/13

Status: Closed
Project: Spring Framework
Component/s: Test
Affects Version/s: 4.0 RC1
Fix Version/s: None

Type: Bug Priority: Minor
Reporter: Dave Syer Assignee: Sam Brannen
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relate
is related to SPR-11292 Document that Servlet mocks are now i... Closed
Days since last comment: 47 weeks ago
Last commented by a User: true
Last updater: Spring Issuemaster

 Description   

This shows up in the Spring OAuth build with Spring 4.0:

java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
	at org.springframework.mock.web.MockHttpSession.<init>(MockHttpSession.java:99)
	at org.springframework.mock.web.MockHttpSession.<init>(MockHttpSession.java:89)
	at org.springframework.mock.web.MockHttpSession.<init>(MockHttpSession.java:80)


 Comments   
Comment by Sam Brannen [ 31/Oct/13 ]

The Servlet API mocks in Spring Framework 4.0 support Servlet 3.0 and higher, as can be seen in GitHub commit deba32cad9.

The reason why MockHttpSession can not be instantiated when running against Servlet 2.x is that the MockHttpSession constructor instantiates a MockServletContext which has a dependency on SessionCookieConfig from Servlet 3.0.

Unfortunately, the Javadoc for MockHttpSession did not get updated with the corresponding GitHub commit.

So I'll update the Javadoc and resolve this issue as Works as Designed.

Regards,

Sam

Comment by Sam Brannen [ 31/Oct/13 ]

Outstanding documentation issues have been addressed as described in the comments for GitHub commit 2e6c998168:

Update Javadoc for mocks regarding Servlet 3.0

Commit deba32cad9 upgraded the Servlet API mocks to Servlet 3.0;
however, not all of the Javadoc was updated accordingly.

This commit updates the remaining Javadoc with regard to Servlet 3.0 as
the baseline for mocks in the spring-test module.

In addition, this commit syncs up the mocks used for internal testing in
the spring-web module with the most current versions in spring-test.

Comment by Franz Neumann [ 13/Dec/13 ]

What about those guys, who can't use Servlet 3.x i.e in Google Appengine it's defined on server as 2.5. Does it mean, that we can't use spring 4 before we changed to Servlet 3?

Comment by Dave Syer [ 13/Dec/13 ]

No, it means you can't use Servlet 2.5 jars for testing. Your app will still run in Appengine.

Comment by Franz Neumann [ 13/Dec/13 ]

Ok, Ive got it later. But, I actually tried to run my integration (unit) tests with Spring 4, but I got the same exception java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig. I think it's because, I use also appengine test-libs for testing and there are also some incompatibilities

java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
	at org.springframework.test.context.web.AbstractGenericWebContextLoader.configureWebResources(AbstractGenericWebContextLoader.java:182)
	at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:123)
	at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
	at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250)
	at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
	at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
	at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:326)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:212)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: javax.servlet.SessionCookieConfig
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
	... 32 more


Comment by Sam Brannen [ 13/Dec/13 ]

As mentioned in the Spring Framework 4.0 Reference Manual...

Deployment to Servlet 2.5 servers remains an option, but Spring Framework 4.0 is now focused primarily on Servlet 3.0+ environments. If you are using the Spring MVC Test Framework you will need to ensure that a Servlet 3.0 compatible JAR is in your test classpath.

Comment by Franz Neumann [ 17/Dec/13 ]

so i found a solution for my problem
just add in pom.xml dependency for Servlet 3.0 with scope test and it shold work
i.e.

	<dependency>
			<groupId>org.apache.geronimo.specs</groupId>
			<artifactId>geronimo-servlet_3.0_spec</artifactId>
			<version>1.0</version>
			<scope>test</scope>
		</dependency>
Comment by Sam Brannen [ 17/Dec/13 ]

Great!

Glad you found a solution, Franz.

Comment by Spring Issuemaster [ 14/Jan/19 ]

The Spring Framework has migrated to GitHub Issues. This issue corresponds to spring-projects/spring-framework#15676.

Generated at Tue Dec 10 11:33:51 UTC 2019 using Jira 7.13.8#713008-sha1:1606a5c1e7006e1ab135aac81f7a9566b2dbc3a6.