Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test code that needs passed HTTPServletRequest
    primarykey
    data
    text
    <p>I'm running some Scala code (with Java socialAuth library) that requires an HTTPServletRequest to be passed in as a param. </p> <pre><code>def myClass(prov: String, site: String, request: HttpServletRequest): { //some initial code here val session = request.getSession() session.setAttribute(/*some params*/) //code continues... } </code></pre> <p>So the HttpServletRequest seems like a really cool way of storing session variables and passing a session between methods. However, I'm not quite sure how to test this code, as I don't really understand what the HttpServletRequest interface <em>is</em>.</p> <p>I've done a bit of research, but I was wondering if anyone could clarify for me. <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html" rel="nofollow">HttpServletRequest</a> is an interface, which means it can't be instantiated on its own. How, then is it used in code? </p> <p>EDIT: as in, when a method takes an HttpServletRequest as a parameter, what implementing class is usually passed in? </p> <hr> <p>FOLLOWUP: Based on the answers given, HttpServletRequest is implemented differently by different servers; Ireeder pointed out, for example, that Apache even has multiple implementations. How, then, does code specify that the server needs to use its implementation? In other words, lets say I have test code that uses myClass:</p> <pre><code>def otherClass(){ val site = "www.example.com" val provider = "twitter" val mockRequest = mock(HttpServletRequest.class) //using https://code.google.com/p/mockito/. (eclipse throws error..not too sure about syntax but throwing up this example quickly, so will edit later) myClass(provider, site, mockRequest) } </code></pre> <p>I assume this is OK for testing, as Mockito creates a mock object, but when otherClass() needs to be implemented on a server, how is its code adjusted? (Sorry, I'm a bit new at this, so bear with me...) If <code>val mockRequest</code> needs to be implemented with whatever HttpServletRequest implementation is used by the server (Apache, Oracle, etc), how does the user specify this in <code>otherClass</code>? Will something like this change:</p> <pre><code>//deleted line: --val mockRequest = mock(HttpServletRequest.class) //changed line: myClass(provider, site, mockRequest) to below: myClass(provider, site, javax.servlet.http.HttpServletRequest) </code></pre> <p>be interpreted and implemented correctly by the server?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload