title: Spring 3 - real integration test
body: <p>currently I am working on a spring based application. I do have some unit tests for many purposes of the application. The problem is, I've not found any solution to do a real integration test on my application.</p>
  
  <p>Let me give you an example:</p>
  
  <p>In my current tests I do setup a request for a specific controller and pass those components to my handler:</p>
  
  <pre><code>// setup request
  request.setMethod("GET");
  request.setRequestURI("/customer");
  request.addHeader("Accept", "application/json");
  request.addHeader("Authorization", "Basic xyz"); // base_64
  handlerAdapter.handle(request, response, myController);
  </code></pre>
  
  <p>But this is not a real integration test, because what I am saying is, okay I do have a request,  and myController should handle this request.</p>
  
  <p>That's a problem, because what I really want to test is the following:</p>
  
  <p>I do have a request, and my application should handle this request. I do not want to specify the controller because in case of a running application and a request to "/customer" spring does dispatch which controller solves my request. 
  I really want to simulate that I am a browser or another client sending requests to my backend.</p>
  
  <p>The reason I want to test this is, that otherwise some kind of tests passes although the tested functionality doesn't work in the "real world"</p>
  
  <p>I could give you this example to understand, in which way the tests differ from the production run:</p>
  
  <p>Let's assume I do have a file upload within a form with a parameter. My controller function is this one:</p>
  
  <pre><code>@RequestMapping(value="/{id}", method = RequestMethod.PUT, headers="content-type=multipart/form-data")
  public ResponseEntity&lt;String&gt; createFromMyForm(@PathVariable("id") Long id, @RequestParam String myParameter, @RequestParam MultipartFile myFile) {
  //...
  }
  </code></pre>
  
  <p>Have a look at the RequestMethod.PUT, this is not working in an production build, because html standard for uploading files is POST.</p>
  
  <p>In my unit test I am doing this:</p>
  
  <pre><code>// setup request
  MockMultipartHttpServletRequest multipartRequest = new MockMultipartHttpServletRequest();
  MockHttpServletResponse multipartResponse = new MockHttpServletResponse();
  
  // Open file
  final FileInputStream fis = new FileInputStream("img/test.png");
  MockMultipartFile multipartFile = new MockMultipartFile(
              "myParameter", fis);
  
  // generate multipart mock
  multipartRequest.addFile(multipartFile);
  multipartRequest.addParameter("json", "test");
  
  // send request
  handlerAdapter.handle(multipartRequest, multipartResponse, controller);
  </code></pre>
  
  <p>Problem: The test passes, while it should not. The reasons this test passes are several. First I am specifing the request. I do not say I have a request, but instead I have to say I have a MultipartRequest. This leads the dispatcher servlet to return true in an function called something like isMultipartRequest(). In the production run it returns false, because there are no multipart requests on PUT methods. Further I specify again the controller which should handle this request.</p>
  
  <p>Maybe there are other examples, but what I want to say is, is there any way to test my application under real conditions including my html protocol and everything else?</p>
  
  <p>I would really appreciate any kind of help!</p>
  
  <p>Thanks in advance</p>