Search code examples
javaspringmavenhttpsession

Create HttpServletRequest with session


I'm trying to coverage my spring application and I found some problems. The principal one its to create a new HttpServletRequest and set session to get and set attributes (to coverage all lines of my code). This tests will run automatically with JUnit throwing by Maven, then i haven't request not response.

I'm looking for this and i found the Mockito API that brings us a way to create HttpServletRequest objects but i can't set a session to that object:

HttpServletRequest req = Mockito.mock(HttpServletRequest.class);

Can someone help me?


Solution

  • Ok, I solved it. The comment of @M. Denium is right, but take warning on the Spring version. At the first attempt, I imported the MockHttpServletRequest of the spring-mock library, and i got a NoSuchMethodError. It's about the Spring version, spring-mock is for Spring 1 and 2. For Spring 3 or 4, use the spring-test library, it will work.