title: Mockito / Spring MVC - (annotation-driven) request mapping dynamic verification
body: <p>I'm currently writing a Spring MVC-based webapp.</p>
  
  <p>Rather than writing one test for every annotated method, I would like to benefit from Parameterized JUnit runner.</p>
  
  <p>Finally, I got it almost working, although <em>I had to change all primitive arguments to their wrapper counterpart</em> in my controller methods (and then manually do the sanity checks on null refs).</p>
  
  <p>If it can help, here is the code (this also depends on Guava):</p>
  
  <pre><code>@RunWith(Parameterized.class)
  public class MyControllerMappingTest {
  
      private MockHttpServletRequest request;
      private MockHttpServletResponse response;
      private MyController mockedController;
      private AnnotationMethodHandlerAdapter annotationHandlerAdapter;
      private final String httpMethod;
      private final String uri;
      private final String controllerMethod;
      private final Class&lt;?&gt;[] parameterTypes;
      private final Object[] parameterValues;
  
      @Before
      public void setup() {
          request = new MockHttpServletRequest();
          response = new MockHttpServletResponse();
          mockedController = mock(MyController.class);
          annotationHandlerAdapter = new AnnotationMethodHandlerAdapter();
      }
  
      @Parameters
      public static Collection&lt;Object[]&gt; requestMappings() {
          return asList(new Object[][] {
                  {"GET", "/my/uri/0", "index", arguments(new MethodArgument(Integer.class, 0))}
          });
      }
  
      private static List&lt;MethodArgument&gt; arguments(MethodArgument... arguments) {
          return asList(arguments);
      }
  
      public MyControllerMappingTest(String httpMethod, String uri, String controllerMethod, List&lt;MethodArgument&gt; additionalParameters) {
          this.httpMethod = httpMethod;
          this.uri = uri;
          this.controllerMethod = controllerMethod;
          this.parameterTypes = new Class&lt;?&gt;[additionalParameters.size()];
          initializeParameterTypes(additionalParameters);
          this.parameterValues = newArrayList(transform(additionalParameters, valueExtractor())).toArray();
  }
  
      private void initializeParameterTypes(List&lt;MethodArgument&gt; additionalParameters) {
          Iterable&lt;Class&lt;?&gt;&gt; classes = transform(additionalParameters, typeExtractor());
          int i = 0;
          for (Class&lt;?&gt; parameterClass : classes) {
              parameterTypes[i++] = parameterClass;
          }
      }
  
      @Test
      public void when_matching_mapping_constraints_then_controller_method_automatically_called() throws Exception {
          request.setMethod(httpMethod);
          request.setRequestURI(uri);
          annotationHandlerAdapter.handle(request, response, mockedController);
  
          Method method = MyController.class.getMethod(controllerMethod, parameterTypes);
          method.invoke(verify(mockedController), parameterValues);
      }
  }
  </code></pre>
  
  <p>with the custom class MethodArgument that follows:</p>
  
  <pre><code>public class MethodArgument {
      private final Class&lt;?&gt; type;
      private final Object value;
  
      public MethodArgument(final Class&lt;?&gt; type, final Object value) {
          this.type = type;
          this.value = value;
      }
  
      public Object getValue() {
          return value;
      }
  
      public Class&lt;?&gt; getType() {
          return type;
      }
  
      public static Function&lt;MethodArgument, Class&lt;?&gt;&gt; typeExtractor() {
          return new Function&lt;MethodArgument, Class&lt;?&gt;&gt;() {
              @Override
              public Class&lt;?&gt; apply(MethodArgument argument) {
                  return argument.getType();
              }
          };
      }
  
      public static Function&lt;MethodArgument, Object&gt; valueExtractor() {
          return new Function&lt;MethodArgument, Object&gt;() {
              @Override
              public Object apply(MethodArgument argument) {
                  return argument.getValue();
              }
          };
      }
  }
  </code></pre>
  
  <p>So, I'm almost there, the only test case here works because of Java Integer cache, and the Integer instance is therefore the same throughout the call chain... This however doesn't work with custom objects, I always end up with an InvocationTargetException (cause: "Argument(s) are different!")...</p>
  
  <p>The types are correct but the passed instances are not identical to the ones set in the @Parameters method.</p>
  
  <p>Any idea how to work around this?</p>